![](/img/trans.png)
[英]Binding ItemsSource of a ComboBoxColumn in WPF DataGrid
[英]Binding ComboBoxColumn to collection from DataGrid's ItemsSource in WPF DataGrid
請幫助我弄清楚如何在WPF的DataGrid中使用ComboBoxColumn。 我正在嘗試創建設備列表,其中每個設備在“ log”字段中都有動態狀態列表。
<DataGrid AutoGenerateColumns="False" Margin="12,6,12,12" Name="dataGrid1" Grid.Row="1" SelectionUnit="FullRow">
<DataGrid.Columns>
...
<DataGridComboBoxColumn Header="Log"
ItemsSource="{Binding log, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Device}}}"/>
</DataGrid.Columns>
</DataGrid>
public partial class MainWindow : Window
{
public ObservableCollection<Device> devices;
...
}
public MainWindow()
{
...
dataGrid1.ItemSource = devices;
}
public class Device : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public Device() {log = new ObservableCollection<string>();}
...
private ObservableCollection<string> _log;
public ObservableCollection<string> log { get { return _log; }
set { _log = value; OnPropertyChanged("log"); } }
}
您能否分享任何建議:如何在每個對象的數據網格列表“日志”中的每個組合框中顯示?
MSDN:DataGridComboboxColumns說:
若要填充下拉列表,請首先使用以下選項之一設置ComboBox的ItemsSource屬性:
- 靜態資源。 有關更多信息,請參見StaticResource標記擴展。
- x:靜態代碼實體。 有關更多信息,請參見x:靜態標記擴展。
- ComboBoxItem類型的嵌入式集合。
因此,基本上只綁定到數據對象的collection屬性,最好使用DataGridTemplateColumn
:
<DataGridTemplateColumn Header="Log">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding log}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
這種類型的列也為您提供了更多的模板可能性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.