简体   繁体   中英

DataGridComboBoxColumn.ItemsSource. Binding vs EditingElementStyle

There are 2 XAML, why the second work, and the first is not?

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 not work:

<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>

Error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=RoutersPortsView; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=11022751); target property is 'ItemsSource' (type 'IEnumerable')

XAML work:

<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>

Expose your DP in the form :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM