简体   繁体   English

是否可以将WPF控件绑定到多个数据上下文?

[英]Is it possible to bind a WPF control to multiple data contexts?

I've written an UserControl whose DataContext contains a collection and an bool property. 我编写了一个UserControl,其DataContext包含一个集合和一个bool属性。 The collection is being displayed (and editited) within a data grid, which has custrom column templates. 该集合在一个具有custrom列模板的数据网格内显示(和编辑)。 The DataContext of a control in a column is of course an item of the collection of the UserControl's DataContext. 列中控件的DataContext当然是UserControl的DataContext集合的一项。 However I need to bind one property of the control in a column to the bool property of the UserControl's DataContext and not to the collection item. 但是,我需要将列中控件的一个属性绑定到UserControl的DataContext的bool属性,而不是集合项。

Do you have any ideas on how to solve this? 您对如何解决这个问题有任何想法吗?

Best Regards, Oliver 最好的问候,奥利弗

I'm pulling this straight from my answer on another post 我正在从另一篇文章的答案中直接得出这个结论

Getting at the "parent" of a databound object? 到达数据绑定对象的“父级”?

Here's the code from the post that I think might pertain to what youre trying to do: 这是我认为可能与您要执行的操作相关的文章代码:

<ListBox Name="list" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding}"/>
                <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext.Values}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Basically the DataContext of the ListBox in this case contains a few lists and a combobox within the DataTemplate of the ItemTemplate is bound to a different member of the DataContext of the ListBox while the ItemsSource is bound to the Items member of DataContext. 基本上,在这种情况下,ListBox的DataContext包含一些列表,并且ItemTemplate的DataTemplate中的组合框绑定到ListBox的DataContext的其他成员,而ItemsSource绑定到DataContext的Items成员。 I would think this could apply to your DataGrid and column templates. 我认为这可能适用于您的DataGrid和列模板。

Solution 1. Create a custom class which contains the collection and a bool property and set the DataContext to an instance of that class. 解决方案1.创建一个包含集合和bool属性的自定义类,并将DataContext设置为该类的实例。

Solution 2. Set the user control's DataContext to the collection and add a bool property to your user control. 解决方案2.将用户控件的DataContext设置为集合,并将bool属性添加到用户控件。

XAML: XAML:

<UserControl x:Class="DataContextDemo.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="self">
    <StackPanel>
        <ListBox ItemsSource="{Binding}" />
        <CheckBox IsChecked="{Binding Path=MyBoolProp, ElementName=self}" />
    </StackPanel>
</UserControl>

Code behind: 后面的代码:

using System.Windows;
using System.Windows.Controls;

namespace DataContextDemo
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public bool MyBoolProp
        {
            get { return (bool)GetValue(MyBoolPropProperty); }
            set { SetValue(MyBoolPropProperty, value); }
        }

        public static readonly DependencyProperty MyBoolPropProperty =
            DependencyProperty.Register("MyBoolProp", 
                                        typeof(bool), 
                                        typeof(UserControl1), 
                                        new UIPropertyMetadata(true));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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