2011-01-07 63 views
2

我想創建一個包含數據網格中的用戶控件,然後直接定義我的用戶裏面列:綁定WPF數據網格中的用戶控件內

<my:ControlContainingDataGrid ItemsSource="{Binding}"> 
    <my:ControlContainingDataGrid.Columns> 
    <DataGridTextColumn Binding="{Binding Path=Property1}" Header="Property 1"/> 

在用戶控件我揭露DataGrid的列:

static ControlContainingDataGrid() 
{ 
    ColumnsProperty = DependencyProperty.Register(
    "Columns", 
    typeof(ObservableCollection<DataGridColumn>), 
    typeof(ControlContainingDataGrid), 
    new UIPropertyMetadata(new ObservableCollection<DataGridColumn>()) 
); 
} 

[Description("Columns"), Category("Columns")] 
public ObservableCollection<DataGridColumn> Columns 
{ 
    get { return _datagGrid.Columns; } 
} 

public static readonly DependencyProperty ColumnsProperty; 

=>不起作用:綁定到Property1的列未創建。

我嘗試以編程方式創建列:

_datagGrid.Columns.Add(new DataGridTextColumn { 
     Header = "Property 1", 
     Binding = new Binding { 
     Path = new PropertyPath("Property1"), 
     Mode = BindingMode.TwoWay, 
     }, 
    }); 

_datagGrid.ItemsSource = testList; 

=>這是行不通的:顯示頭,但我的DataGrid的每一行是空的(壞的綁定?)。


1 - 什麼是通過在XAML部分的用戶控件綁定一個DataGrid的列的simpliest方式?

2-什麼是通過編程方式通過UserControl綁定DataGrid的列的最簡單的方法?

+0

你能發佈你的UserControl的XAML嗎? – 2011-01-07 19:58:22

回答

0

我以編程方式做到這一點。幾乎和你一樣的代碼。工作正常。我從來沒有嘗試綁定Columns集合本身,雖然我不明白爲什麼這不應該工作。

您的消息有點含糊不清。如果你的意思是說你的專欄沒有出現,也許你需要在創建表之前添加專欄。如果這些行在列中顯示爲空值,那麼您的綁定'Property1'是錯誤的。

我不在XAML中工作,所以對此一無所知。