gpt4 book ai didi

.net - 将 MultiBinding 与 TemplateBindings 结合使用

转载 作者:行者123 更新时间:2023-12-02 11:25:29 25 4
gpt4 key购买 nike

我正在 WPF 中制作自定义控件。我仍在学习 TemplateBinding 的详细信息(在自定义控件中大量使用)。

我注意到我似乎无法在 MulitBinding 内部使用 TemplateBinding。

当我尝试这个时:

<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource MyMultiConverter}">
<Binding ElementName="PART_AComboBox" Path="SelectedItem"/>
<TemplateBinding Property="MyListOne"/>
<TemplateBinding Property="MyListTwo"/>
</MultiBinding>
</ComboBox.ItemsSource>

我收到此错误:

The value "System.Windows.TemplateBindingExpression" is not of type "System.Windows.Data.BindingBase" and cannot be used in this generic collection.
Parameter name: value

我错过了什么吗?有办法让它工作吗?

这是我正在采取的解决方法,但它是一种黑客:

<ListBox x:Name="ListOne" 
ItemsSource="{TemplateBinding MyListOne}"
Visibility="Collapsed" />
<ListBox x:Name="ListTwo"
ItemsSource="{TemplateBinding MyListTwo}"
Visibility="Collapsed" />

<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource DictionaryFilteredToKeysConverter}">
<Binding ElementName="PART_TextTemplateAreasHost" Path="SelectedItem"/>
<Binding ElementName="ListOne" Path="ItemsSource"/>
<Binding ElementName="ListTwo" Path="ItemsSource"/>
</MultiBinding>
</ComboBox.ItemsSource>

我将 ListBoxes 绑定(bind)到依赖属性,然后在我的多重绑定(bind)中将一个元素绑定(bind)到列表框的 ItemsSource。

正如我上面所说,这感觉像是一种黑客行为,我想知道是否有正确的方法来使用 TemplateBinding 作为组件之一来执行多重绑定(bind)。

最佳答案

您可以使用:

<Binding Path="MyListOne" RelativeSource="{RelativeSource TemplatedParent}"/>

TemplateBinding 实际上只是上述内容的简化、优化版本。它对于使用地点和方式非常严格(直接在模板内部,没有分层路径等)。

XAML 编译器在对此类问题提供适当的反馈方面仍然相当垃圾(至少在 4.0 中,没有专门为此测试 4.5)。我刚刚有了这个 XAML:

<ControlTemplate TargetType="...">
<Path ...>
<Path.RenderTransform>
<RotateTransform Angle="{TemplateBinding Tag}"/>
</Path.RenderTransform>
</Path>
</ControlTemplate>

它编译并执行得很好,但没有将 Tag 中的值绑定(bind)到旋转角度。我窥探了一下,发现该属性(property)已绑定(bind),但为零。直觉上(经过多年处理这种烦恼)我将其更改为:

<ControlTemplate TargetType="...">
<Path ...>
<Path.RenderTransform>
<RotateTransform Angle="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
</Path.RenderTransform>
</Path>
</ControlTemplate>

而且效果很好。

关于.net - 将 MultiBinding 与 TemplateBindings 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14342644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com