【问题标题】:How to set the sources in this multibinding example?如何在这个多重绑定示例中设置源?
【发布时间】:2015-03-24 22:11:19
【问题描述】:

我有 2 个这样的课程:

public MyClass1: INotifyValueChanged
{
    public Dictionary<int, Color> Property1 
    { 
        public event PropertyChangedEventHandler PropertyChanged;

        get
        { 
             return this.property1
        }
        set
        {
             PropertyChanged.ChangeAndNotify(ref this.property1, value, () => Property1);
        }
    }
}

public MyClass2: INotifyValueChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public int Property2
    {   get
        { 
             return this.property2
        }
        set
        {
             PropertyChanged.ChangeAndNotify(ref this.property2, value, () => Property2);
        }
    }
}

ChangeAndNotify 是我使用的扩展方法,而不是普通语法来通知属性更改,因为这样我不需要将要更改的属性的名称键入为字符串,所以我认为它与问题无关。有需要我会发的。

我想将 MyClass2.Property2 绑定到 Rectangle.Fill。

为此,我必须创建一个 IMultiValueConverter,它将在 Property1 的字典中查找 Property2 并返回它的颜色。

在我的 XAML 中,我为转换器类创建了一个条目:

<local:MaterialConverter x:Key="MaterialsConverter" />

然后我尝试对 Rectangle 进行多重绑定:

<Rectangle>
    <Rectangle.Fill>
        <SolidColorBrush>
            <SolidColorBrush.Color>
                <MultiBinding Converter="{StaticResource MaterialsConverter}">
                    <Binding Path=Property1 />
                    <Binding Path=Property2 />
                </MultiBinding>
            </SolidColorBrush.Color>
        </SolidColorBrush>
     <Rectangle.Fill/>
</Rectangle>

在表单代码中,我有这 2 个类的 2 个变量:分别是 classObj1 和 classObj2。

在初始化和设置它们之后,我这样做是为了绑定:

rectangle.DataContext = class1Obj;

当然它不起作用,因为它是一个多重绑定,我在 DataContext 中只指定了 1 个元素,我收到一个错误,说 MyClass1 上不存在 Property2。

我不认为我可以将 2 个 DataContexts 设置为一个对象,但我可以以某种方式设置 XAML 中 2 个绑定之一的 Source 属性,因此一个绑定将来自矩形的 DataContext,另一个将从这个另一个地方来。但是我可以在哪里放置 class2Obj 或如何放置?

【问题讨论】:

  • MultiBinding 中设置绑定时,您不能单独为每个绑定设置Source 属性,而不是继承DataContext
  • ChangeAndNotify 是一种扩展方法,我使用它代替普通语法来通知属性已更改,因为这样我不需要将要更改的属性的名称键入为字符串 ...无论如何您都不需要...请参阅 MSDN 上的 CallerMemberNameAttribute 页面。

标签: c# wpf multibinding


【解决方案1】:

这是一个可以帮助您的示例。让我们从 2 个简单的类开始:

public class A
{
    public string Name { get; set; }
}

public class B
{
    public Dictionary<string, string> Dict { get; set; }
}

让我们用以下方式初始化DataContext

window.DataContext = new object[]
{
    new A{ Name = "Hello!" }, 
    new B
    { 
        Dict =new Dictionary<string, string> { "1", "a"}, {"2", "b"} 
    }
};

现在让我们创建一个转换器。我假设 values 数组中的第一个对象是 A 类型,第二个是 B 类型:

public class MultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var sb = new StringBuilder();
        sb.AppendLine(values[0].ToString());
        foreach (var kvp in (Dictionary<string, string>) values[1])
            sb.AppendLine(kvp.Key + "-" + kvp.Value);
        return sb.ToString();
    }
    //...
}

最后是 XAML:

<Window.Resources>
    <local:MultiConverter x:Key="converter"></local:MultiConverter>
</Window.Resources>
<TextBlock Name="textBox2">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource converter}">
            <Binding Path="[0].Name"/>
            <Binding Path="[1].Dict"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

【讨论】:

    【解决方案2】:

    您可以使用 Steven Rands 的建议:

    <SolidColorBrush.Color>
        <MultiBinding Converter="{StaticResource MaterialsConverter}">
            <Binding Path="Property1" Source="{StaticResource MyClass1}" />
            <Binding Path="Property2" Source="{StaticResource MyClass2}" />
        </MultiBinding>
    </SolidColorBrush.Color>
    

    或者你可以使用第三类作为你的矩形的DataContext

    public class MyClass3 : INotifyPropertyChanged
    {
        public MyClass1 Class1
        {
            get
            {
                /* your code... */
            }
            set
            {
                /* your code... */    
            }
        }
    
        public MyClass2 Class2
        {
            get
            {
                /* your code... */
            }
            set
            {
                /* your code... */
            }
        }
    }
    

    在这种情况下,您的绑定将变为:

    <Binding Path="Class1.Property1" />
    <Binding Path="Class2.Property2" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2012-04-10
      • 2018-10-26
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多