通过 MultiBinding 将不正确的值传递到 MultiValueConverter
Passing incorrect values into MultiValueConverter by MultiBinding
我一直在尝试为 RadioButton 实现动态工具提示 (WPF)(当 RadioButton 的 IsEnabled 更改时,工具提示会切换)。我想用 MultiValueConverter 来实现这一点,它是一种通用转换器,它接受 3 个值 - IsEnabled 值,按此确切顺序启用 ToolTip 和禁用 ToolTip。
但遗憾的是我遇到了一个问题,我还没有解决。基本上,当代码到达 Convert 方法时,值数组将填充 DependencyProperty.UnsetValue 项。
我在谷歌搜索时设法找到的是,问题可能是由这里提到的错误的 DataContext 引起的 WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue,但我觉得我已经尝试了 RelativeSources 和 DataContexts 的所有组合,我可以想出但没有任何帮助。
视图的示例代码如下:
<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>
因此,我期望的结果是正确的值将传递到转换器(在本例中为 IsEnabled 的值和 Enabled/Disabled 工具提示的字符串值)。
如果有人有任何想法,我将不胜感激:)。
提前致谢。
我设法通过在 RadioButton 上显式设置 DataContext 并删除 MultiBinding 中的 RelativeSources 来解决这个问题。虽然我不明白为什么它不能与 RelativeSources 一起使用,但它可以工作。这是代码,以防将来有人阅读此代码:
<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>
我一直在尝试为 RadioButton 实现动态工具提示 (WPF)(当 RadioButton 的 IsEnabled 更改时,工具提示会切换)。我想用 MultiValueConverter 来实现这一点,它是一种通用转换器,它接受 3 个值 - IsEnabled 值,按此确切顺序启用 ToolTip 和禁用 ToolTip。
但遗憾的是我遇到了一个问题,我还没有解决。基本上,当代码到达 Convert 方法时,值数组将填充 DependencyProperty.UnsetValue 项。
我在谷歌搜索时设法找到的是,问题可能是由这里提到的错误的 DataContext 引起的 WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue,但我觉得我已经尝试了 RelativeSources 和 DataContexts 的所有组合,我可以想出但没有任何帮助。
视图的示例代码如下:
<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>
因此,我期望的结果是正确的值将传递到转换器(在本例中为 IsEnabled 的值和 Enabled/Disabled 工具提示的字符串值)。
如果有人有任何想法,我将不胜感激:)。
提前致谢。
我设法通过在 RadioButton 上显式设置 DataContext 并删除 MultiBinding 中的 RelativeSources 来解决这个问题。虽然我不明白为什么它不能与 RelativeSources 一起使用,但它可以工作。这是代码,以防将来有人阅读此代码:
<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>