[英]Understand Binding DataGridComboBoxColumn Itemssource and SelectedItem
[英]DataGridComboBoxColumn.ItemsSource. Binding vs EditingElementStyle
XAML有2個,為什么第二個可以工作,而第一個沒有?
CS:
public partial class myClass: Window
{
public static DependencyProperty RoutersPortsViewProperty = DependencyProperty.Register("RoutersPortsView", typeof(DataView), typeof(myClass));
public myClass()
{
DataTable MyTable = new DataTable();
/*here fill MyTable*/
SetValue(RoutersPortsViewProperty, new DataView(MyTable);
InitializeComponent();
}
/*there other code for class*/
}
XAML不起作用:
<Window Name="myName" x:Class="myClass">
<DataGrid>
<DataGrid.Columns>
<DataGridComboBoxColumn DisplayMemberPath="DisplayString"
SelectedValuePath="id"
SelectedValueBinding="{Binding Path=NWPatchPanelID, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
ItemsSource="{Binding Path=RoutersPortsView, ElementName=myName}"/>
</DataGrid.Columns>
</DataGrid>
</Window>
錯誤:
System.Windows.Data錯誤:2:找不到目標元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path = RoutersPortsView; DataItem = null; 目標元素是'DataGridComboBoxColumn'(HashCode = 11022751); 目標屬性為“ ItemsSource”(類型為“ IEnumerable”)
XAML工作:
<Window Name="myName" x:Class="myClass">
<DataGrid>
<DataGrid.Columns>
<DataGridComboBoxColumn DisplayMemberPath="DisplayString"
SelectedValuePath="id"
SelectedValueBinding="{Binding Path=NWPatchPanelID, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=RoutersPortsView, ElementName=myName}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
以以下形式公開您的DP:
public DataView RoutersPortsView
{
get { return (DataView )GetValue(RoutersPortsViewProperty ); }
set { SetValue(RoutersPortsViewProperty , value); }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.