以编程方式在DataGrid中的ItemsSource中设置Binding

时间:2014-07-15 16:36:50

标签: c# wpf binding datagrid

我有下一个XAML代码:

<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 XPath=@Name}"/>
                    <DataGridTextColumn Header="Type" Width="80" Binding="{Binding XPath=@Type}"/>
                    <DataGridTextColumn Header="Count" Binding="{Binding XPath=@Count}"/>
                    <DataGridTextColumn Header="ID" Width="80" Binding="{Binding XPath=@ID}"/>
                </DataGrid.Columns>                    
            </DataGrid>

如何在C#代码中使用Binding设置XPath="Setting/Element[@Name='...']/Field"

1 个答案:

答案 0 :(得分:0)

您可以像这样添加绑定。

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

    some_name.SetBinding(ItemsControl.ItemsSourceProperty, binding);