0
votes

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>
1
Did you create wrapper for RoutersPortsViewProperty? - Spawn
@Spawn Of course! Then have a second option did not work too =) - Дмитрий Чистик
Try Set the value after the InitializeComponent(); - Dinesh balan
@ДмитрийЧистик Set the DataContext of window as this.DataContext = this in the constructor after InitializeComponent, and itemsssource as ItemsSource="{Binding Path=RoutersPortsView}. - AnjumSKhan
Also note that static fields should be initialized in static constructors only. - AnjumSKhan

1 Answers

0
votes

Expose your DP in the form :

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