绑定到一个依赖属性是在父母的DataContext [英] Bind to a Dependency Property that is in parent's DataContext

查看:341
本文介绍了绑定到一个依赖属性是在父母的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想第三列绑定到是窗口的DataContext的范围内,而不是里面的 Col​​lectionBindingOne Col​​lectionBindingTwo 属性C $ C>。

通过定义里面的第二个集合<数据网格> WPF假定的ItemsSource( Col​​lectionBindingOne的项目内的局部范围或东西,以及属性点)。

 <数据网格DockPanel.Dock =顶部的ItemsSource ={结合CollectionBindingOne}的AutoGenerateColumns =FALSE>
    < D​​ataGridTextColumn标题=一绑定={结合PropOne}/>
    < D​​ataGridTextColumn标题=两个绑定={结合PropTwo}/>
    < D​​ataGridComboBoxColumn标题=三公的ItemsSource ={结合CollectionBindingTwo}/>
< / DataGrid的>

例如,这个工作是因为组合框不是&LT内部,数据网格>

 <组合框IsEditable =真的ItemsSource ={结合CollectionBindingTwo}>< /组合框>


解决方案

该DataGridComboBoxColumn是不可视树所以平时的RelativeSource /约束力的ElementName格式将无法正常工作的一部分。您可以通过定义使用变通的<一个href=\"http://stackoverflow.com/questions/5409259/binding-itemssource-of-a-comboboxcolumn-in-wpf-datagrid\">ElementStyle和EditingStyle 的那些在哪里绑定格式将工作。另一种选择是使用一个BindingProxy我使用其他斑点和将节省一些XAML当没有其他原因以限定ElementStyle / EditingStyle

这是BindingProxy类从可冻结继承。

 公共类BindingProxy:可冻结
{
    的可冻结#区域覆盖    保护覆盖可冻结CreateInstanceCore()
    {
        返回新BindingProxy();
    }    #endregion    公共对象数据
    {
        {返回(对象)的GetValue(DataProperty); }
        集合{的SetValue(DataProperty,值); }
    }    //使用的DependencyProperty作为后备存储的数据。
    //这使得动画,造型,装订等..
    公共静态只读的DependencyProperty DataProperty =
        DependencyProperty.Register(数据,
                                    typeof运算(对象),
                                    typeof运算(BindingProxy)
                                    新UIPropertyMetadata(NULL));
}

现在你的XAML是这样的:

 &LT;数据网格DockPanel.Dock =顶部
          的ItemsSource ={结合CollectionBindingOne}
          的AutoGenerateColumns =FALSE&GT;
    &LT; D​​ataGrid.Resources&GT;
        &LT;帮助:Bin​​dingProxy X:键=代理
                             数据={结合}/&GT;
    &LT; /DataGrid.Resources>
    &LT; D​​ataGrid.Columns&GT;
        &LT; D​​ataGridTextColumn标题=一
                            绑定={结合PropOne}/&GT;
        &LT; D​​ataGridTextColumn标题=二
                            绑定={结合PropTwo}/&GT;
        &LT; D​​ataGridComboBoxColumn标题=三公
                                的ItemsSource ={结合Data.CollectionBindingTwo,
                                              来源= {StaticResource的代理}}/&GT;
&LT; / DataGrid的&GT;

不要忘了你的窗口/用户控件的顶部申报帮手命名空间导入。

I would like to bind third column to a CollectionBindingTwo property that is within Window's DataContext and not inside DataContext of an Items from CollectionBindingOne.

By defining the second collection inside <DataGrid> WPF assumes local scope or something, and points to a property within Items of ItemsSource (CollectionBindingOne).

<DataGrid DockPanel.Dock="Top" ItemsSource="{Binding CollectionBindingOne}" AutoGenerateColumns="False">
    <DataGridTextColumn Header="One" Binding="{Binding PropOne}"/>
    <DataGridTextColumn  Header="Two" Binding="{Binding PropTwo}"/>
    <DataGridComboBoxColumn Header="Three" ItemsSource="{Binding CollectionBindingTwo}"/>
</DataGrid>

For example, this works because ComboBox is not inside a <DataGrid>:

<ComboBox IsEditable="True" ItemsSource="{Binding CollectionBindingTwo}"></ComboBox>

解决方案

The DataGridComboBoxColumn is not a part of the Visual Tree so the usual RelativeSource/ElementName binding formats won't work. You can use a workaround by defining the ElementStyle and EditingStyle where those binding formats will work. Another option is to use a BindingProxy which I use for other spots and will save some XAML when there is no other reason to define an ElementStyle/EditingStyle.

This is the BindingProxy class which inherits from Freezable.

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data",
                                    typeof(object),
                                    typeof(BindingProxy),
                                    new UIPropertyMetadata(null));
}

Now your xaml looks like this:

<DataGrid DockPanel.Dock="Top"
          ItemsSource="{Binding CollectionBindingOne}"
          AutoGenerateColumns="False">
    <DataGrid.Resources>
        <helper:BindingProxy x:Key="proxy"
                             Data="{Binding }" />
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="One"
                            Binding="{Binding PropOne}" />
        <DataGridTextColumn Header="Two"
                            Binding="{Binding PropTwo}" />
        <DataGridComboBoxColumn Header="Three" 
                                ItemsSource="{Binding Data.CollectionBindingTwo,
                                              Source={StaticResource proxy}}" />
</DataGrid>

Don't forget to declare the helper namespace import at the top of your Window/UserControl.

这篇关于绑定到一个依赖属性是在父母的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆