wpf 如何在这个多绑定示例中设置源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28151112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the sources in this multibinding example?
提问by RBasniak
I have 2 classes like this:
我有两个这样的课程:
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 is an extension method I use instead of the normal syntax to notify a property changed bevause this way I dont need to type the name of the property being changed as a string, so I think it's not relevant for the problem. If need I'll post it.
ChangeAndNotify 是我使用的一种扩展方法,而不是使用普通语法来通知属性更改,因为这样我不需要将要更改的属性的名称输入为字符串,所以我认为它与问题无关。如果需要我会发布它。
I want to bind MyClass2.Property2 to an Rectangle.Fill.
我想将 MyClass2.Property2 绑定到 Rectangle.Fill。
To do this I have to create an IMultiValueConverter that will look for Property2 on the Dictionary of Property1 and return it's color.
为此,我必须创建一个 IMultiValueConverter,它将在 Property1 的字典中查找 Property2 并返回它的颜色。
In my XAML I created an entry to my converter class:
在我的 XAML 中,我为我的转换器类创建了一个条目:
<local:MaterialConverter x:Key="MaterialsConverter" />
Then I tried to multibind the Rectangle:
然后我尝试多重绑定矩形:
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource MaterialsConverter}">
<Binding Path=Property1 />
<Binding Path=Property2 />
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
<Rectangle.Fill/>
</Rectangle>
On the form code I have 2 variables of these 2 classes: classObj1 and classObj2 respectively.
在表单代码上,我有这 2 个类的 2 个变量:分别是 classObj1 和 classObj2。
After initialise and setup them I do this to bind:
初始化并设置它们后,我这样做是为了绑定:
rectangle.DataContext = class1Obj;
Of course it doesn't work because it's a multibinding and I have specified only 1 element in DataContext and I got an error saying that Property2 does not exist on MyClass1.
当然它不起作用,因为它是一个多重绑定,我在 DataContext 中只指定了 1 个元素,我收到一个错误,说 MyClass1 上不存在 Property2。
I don't think I can set 2 DataContexts to an object, but I could somehow set the Source property of one of the 2 bindings in the XAML, so one binding would come from the DataContext of the rectangle and the other would come from this another place. But where could I put the class2Obj or how?
我不认为我可以为一个对象设置 2 个 DataContext,但我可以以某种方式设置 XAML 中 2 个绑定之一的 Source 属性,因此一个绑定将来自矩形的 DataContext,另一个将来自此另一个地方。但是我可以把 class2Obj 放在哪里或者怎么放?
采纳答案by Micha? Komorowski
Here is an example that should help you. Let's start with 2 simple classes:
这是一个应该对您有所帮助的示例。让我们从 2 个简单的类开始:
public class A
{
public string Name { get; set; }
}
public class B
{
public Dictionary<string, string> Dict { get; set; }
}
Let's initialize DataContextin the following way:
让我们通过以下方式初始化DataContext:
window.DataContext = new object[]
{
new A{ Name = "Hello!" },
new B
{
Dict =new Dictionary<string, string> { "1", "a"}, {"2", "b"}
}
};
Now let's create a converter. I assume that the first object in the valuesarray is of type Aand the second is of type 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();
}
//...
}
And finally here is XAML:
最后是 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>
回答by Il Vic
You can either use Steven Rands's suggestion:
您可以使用 Steven Rands 的建议:
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource MaterialsConverter}">
<Binding Path="Property1" Source="{StaticResource MyClass1}" />
<Binding Path="Property2" Source="{StaticResource MyClass2}" />
</MultiBinding>
</SolidColorBrush.Color>
or you can use a third class as DataContextof your Rectangle:
或者您可以使用DataContext矩形的第三个类:
public class MyClass3 : INotifyPropertyChanged
{
public MyClass1 Class1
{
get
{
/* your code... */
}
set
{
/* your code... */
}
}
public MyClass2 Class2
{
get
{
/* your code... */
}
set
{
/* your code... */
}
}
}
In this case your bindings will become:
在这种情况下,您的绑定将变为:
<Binding Path="Class1.Property1" />
<Binding Path="Class2.Property2" />

