使用带有TemplateBindings的MulitBinding

Vac*_*ano 8 .net wpf xaml templatebinding

我正在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>
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

值"System.Windows.TemplateBindingExpression"不是"System.Windows.Data.BindingBase"类型,不能在此通用集合中使用.
参数名称: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>
Run Code Online (Sandbox Code Playgroud)

我将ListBoxes绑定到依赖项属性,然后在我的mulitbinding中,我将一个元素绑定到列表框的ItemsSource.

正如我上面所说,这感觉就像一个黑客,我想知道是否有一个正确的方法来使用TemplateBinding作为组件之一进行MultiBinding.

Ken*_*art 22

你可以使用:

<Binding Path="MyListOne" RelativeSource="{RelativeSource TemplatedParent}"/>
Run Code Online (Sandbox Code Playgroud)

TemplateBinding实际上只是上述的简易优化版本.在何处以及如何使用它是非常严格的(直接在没有分层路径的模板内部等).

XAML编译器在提供关于这类问题的正确反馈方面仍然非常垃圾(至少在4.0中,没有专门为此测试4.5).我刚才有这个XAML:

<ControlTemplate TargetType="...">
    <Path ...>
        <Path.RenderTransform>
            <RotateTransform Angle="{TemplateBinding Tag}"/>
        </Path.RenderTransform>
    </Path>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

它编译并执行正常,但没有将值绑定Tag到旋转角度.我偷偷摸摸地看到该物业已被绑定,但为零.直觉(经过多年处理这种烦恼之后)我改为:

<ControlTemplate TargetType="...">
    <Path ...>
        <Path.RenderTransform>
            <RotateTransform Angle="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
        </Path.RenderTransform>
    </Path>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

它工作得很好.