C# 如何在组合框TemplateSelector中强制WPF多绑定更新?
作为前言,这个问题来自于对如何使所选项目看起来与组合框中的下拉项目不同的扩展 我试图使我的自定义选定项使用组合框的标记属性中的信息。因为我需要使用一个转换器,所以我正在使用一个,以便能够将“我自己”发送到转换类。因此,我有两个模板:C# 如何在组合框TemplateSelector中强制WPF多绑定更新?,c#,wpf,xaml,data-binding,imultivalueconverter,C#,Wpf,Xaml,Data Binding,Imultivalueconverter,作为前言,这个问题来自于对如何使所选项目看起来与组合框中的下拉项目不同的扩展 我试图使我的自定义选定项使用组合框的标记属性中的信息。因为我需要使用一个转换器,所以我正在使用一个,以便能够将“我自己”发送到转换类。因此,我有两个模板: <DataTemplate x:Key="SelectedItemTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock>
<DataTemplate x:Key="SelectedItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SelectedConverter}">
<Binding RelativeSource="{RelativeSource Self}" />
<Binding StringFormat="This is here so it's called every time" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="DropDownItemsTemplate">
<TextBlock Grid.Column="0" Text="{Binding}" Margin="0,0,10,0" VerticalAlignment="Center" />
</DataTemplate>
组合框的XAML
<ComboBox Name="myComboBox" Grid.Column="0" Tag="My Custom Tag" ItemTemplateSelector="{StaticResource CbTemplateSelector}">
<ComboBox.Items>
<ComboBoxItem Content="A" />
<ComboBoxItem Content="B" />
<ComboBoxItem Content="C" />
<ComboBoxItem Content="D" />
<ComboBoxItem Content="E" />
</ComboBox.Items>
</ComboBox>
所有这些都很好地产生了:
现在,我需要做的是,当我更改Tag属性时,将“CustomStuff C”更改为其他内容。到目前为止,它只会在我更改所选项目时更改,但我需要更改它,而用户不必将其更改为D,然后再更改回C
在reserach中,我尝试用下面的代码强制更新,但不起作用
myComboBox.GetBindingExpression(ComboBox.ItemTemplateProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.ItemTemplateSelectorProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.SelectionBoxItemTemplateProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.TemplateProperty)?.UpdateTarget();
myComboBox.UpdateLayout();
myComboBox.OnSelectionChanged(new SelectionChangedEventArgs(SelectionChangedEvent, new List<bool> { }, new List<bool> { }));
myComboBox.GetBindingExpression(ComboBox.ItemTemplateProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.ItemTemplateSelectorProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.SelectionBoxItemTemplateProperty)?.UpdateTarget();
myComboBox.GetBindingExpression(ComboBox.TemplateProperty)?.UpdateTarget();
myComboBox.UpdateLayout();
OnSelectionChanged(新SelectionChangedEventArgs(SelectionChangedEvent,新列表{},新列表{}));
请注意,实际上我有一个从组合框继承的自定义控件,因此我可以访问受保护的方法(这就是上面最后一行代码的工作方式)。我只是把那部分提取出来,以便得到一小部分可复制的代码
在这一点上,我不知道还有什么可以尝试。那么,如何根据我的操作方式强制更新绑定?不要绑定到self,然后在代码中搜索父combobox,只需使用内置方法即可:
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SelectedConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}" Path="Tag" />
<Binding StringFormat="This is here so it's called every time" />
</MultiBinding>
</TextBlock.Text>
另外,如果您直接绑定到ComboBox.Tag,则当该标记更改时,整个多重绑定将被刷新。尝试直接绑定到ComboBox.Tag,如下所示:{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ComboBox},Path=Tag}@Evk,我不能这样做,因为我不想要标记中的实际内容。我想用标签上的东西做点别的。我理解,但我的意思是,不使用多重绑定中的“”而直接绑定到如上所述的标记(但仍然使用多重绑定)。虽然不确定为什么要使用多重绑定,但在转换器中,您只使用第一个参数,所以您也可以对转换器使用常规绑定。要点是:是否使用多重绑定或常规转换器-如果您(在多个绑定中的一个)直接绑定到ComboBox。当此标记更改时,将刷新标记绑定。@Evk,请立即尝试。初步调查似乎正在进行中。想让你的回答吗?
<TextBlock.Text>
<MultiBinding Converter="{StaticResource SelectedConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}" Path="Tag" />
<Binding StringFormat="This is here so it's called every time" />
</MultiBinding>
</TextBlock.Text>