[英]Passing incorrect values into MultiValueConverter by MultiBinding
I've been trying to implement a dynamic ToolTip (WPF) for RadioButton (the ToolTip switches, when IsEnabled of the RadioButton changes). 我一直在尝试为RadioButton实现动态工具提示(WPF)(当RadioButton的IsEnabled更改时,工具提示会切换)。 I wanted to achieve this with a MultiValueConverter, which would be sort of a general Converter, that accepts 3 values - the IsEnabled value, enabled ToolTip and disabled ToolTip in this exact order.
我想用MultiValueConverter来实现这一点,MultiValueConverter有点像一个普通的Converter,它接受3个值-IsEnabled值,启用的ToolTip和禁用的ToolTip以此顺序排列。
But sadly I have encountered an issue, that I haven't been able to solve yet. 但是可悲的是我遇到了一个问题,我还无法解决。 Basically when the code reaches the Convert Method, the Array of values is filled with DependencyProperty.UnsetValue items.
基本上,当代码到达Convert方法时,值数组将填充DependencyProperty.UnsetValue项目。
What I managed to find while googling was, that the problem is propably caused by having a wrong DataContext as mentioned here WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue , but I feel like I have tried every combination of RelativeSources and DataContexts, that I could come up with and nothing helped. 我在谷歌搜索时设法找到的问题是,问题可能是由错误的DataContext引起的,如此处提到的。Converter中的WPF MultiBinding失败==> DependencyProperty.UnsetValue ,但是我感觉我已经尝试了RelativeSources和DataContexts的每种组合,我想出了,没有任何帮助。
Here is the sample code of the View: 这是视图的示例代码:
<window.Resources>
<local:BooleanToStringConverter x:Key="BooleanToStringConverter"/>
</window.Resources>
<Grid>
<RadioButton x:Name="RadioButton" ToolTipService.ShowOnDisabled="True"
IsEnabled="{Binding IsRadioButtonEnabled}" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Radio">
<RadioButton.ToolTip>
<ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource BooleanToStringConverter}">
<Binding ElementName="RadioButton" Path="IsEnabled"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type local:MainWindow}}" Path="ViewModel.EnabledToolTip"/>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type local:MainWindow}}" Path="ViewModel.DisabledToolTip"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTip>
</RadioButton.ToolTip>
</RadioButton>
So the result, that I expect from this, is that the correct values will be passed into the Converter (in this case value of IsEnabled and string values of Enabled/Disabled ToolTips). 因此,我希望从中得到的结果是正确的值将传递到Converter(在这种情况下,为IsEnabled的值和Enabled / Disabled ToolTips的字符串值)。
If anybody has any ideas, I would very much appreciate to hear them :). 如果有人有任何想法,我将不胜感激:)。
Thanks in advance. 提前致谢。
I managed to fix this by explicitly setting DataContext on the RadioButton and removing the RelativeSources within the MultiBinding. 我设法通过在RadioButton上显式设置DataContext并删除MultiBinding中的RelativeSources来解决此问题。 Though I don't understand why it did not work with the RelativeSources, it works.
尽管我不明白为什么它不能与RelativeSources一起使用,但是它可以工作。 Here is the code, in case of anyone reading this in the future:
这是代码,以防将来有人阅读:
<RadioButton x:Name="RadioButton" ToolTipService.ShowOnDisabled="True"
DataContext="{Binding ViewModel, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"
IsEnabled="{Binding IsRadioButtonEnabled}" VerticalAlignment="Center" HorizontalAlignment="Center" Content="Radio">
<RadioButton.ToolTip>
<ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource BooleanToStringConverter}">
<Binding Path="IsRadioButtonEnabled"/>
<Binding Path="EnabledToolTip"/>
<Binding Path="DisabledToolTip"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTip>
</RadioButton.ToolTip>
</RadioButton>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.