DataGridComboBoxColumn.ItemsSource。绑定与EditingElementStyle

时间:2015-11-30 07:13:43

标签: c# wpf xaml

有2个XAML,为什么第二个工作,第一个不工作?

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:路径= 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>

1 个答案:

答案 0 :(得分:0)

以下列形式公开您的DP:

public DataView RoutersPortsView
        {
            get { return (DataView )GetValue(RoutersPortsViewProperty ); }
            set { SetValue(RoutersPortsViewProperty , value); }
        }
相关问题