2014-07-15 108 views
-1

我的下一個XAML代碼:在設置綁定的ItemsSource DataGrid中programmaticaly

<DataGrid x:Name="some_name" AutoGenerateColumns="False"> 
       <DataGrid.ItemsSource> 
        <Binding XPath="Setting/Element[@Name='...']/Field"/> 
       </DataGrid.ItemsSource> 
       <DataGrid.Columns> 
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding [email protected]}"/> 
        <DataGridTextColumn Header="Type" Width="80" Binding="{Binding [email protected]}"/> 
        <DataGridTextColumn Header="Count" Binding="{Binding [email protected]}"/> 
        <DataGridTextColumn Header="ID" Width="80" Binding="{Binding [email protected]}"/> 
       </DataGrid.Columns>      
      </DataGrid> 

我如何在C#代碼中設置BindingXPath="Setting/Element[@Name='...']/Field"

回答

0

您可以像這樣添加綁定。

var binding = new Binding 
    { 
     Source = _viewModel, 
     XPath = "Setting/Element[@Name='...']/Field" 
    }; 

    some_name.SetBinding(ItemsControl.ItemsSourceProperty, binding); 
+0

謝謝你,它工作得很好=) – kodwi

相關問題