【问题标题】:Passing incorrect values into MultiValueConverter by MultiBinding通过 MultiBinding 将不正确的值传递给 MultiValueConverter
【发布时间】:2019-12-23 08:52:39
【问题描述】:

我一直在尝试为 RadioButton 实现动态工具提示 (WPF)(当 RadioButton 的 IsEnabled 发生变化时,工具提示会切换)。我想用一个 MultiValueConverter 来实现这一点,这将是一个通用的转换器,它接受 3 个值 - IsEnabled 值、启用的 ToolTip 和禁用的 ToolTip 以这个确切的顺序。

但遗憾的是,我遇到了一个我还没有解决的问题。基本上,当代码到达 Convert 方法时,值的数组会被 DependencyProperty.UnsetValue 项填充。

我在谷歌搜索时发现,问题可能是由此处提到的错误 DataContext 引起的 WPF MultiBinding in Convertor fails ==> DependencyProperty.UnsetValue ,但我觉得我已经尝试了 RelativeSources 和 DataContexts 的所有组合,我可以想出没有任何帮助。

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>

因此,我期望的结果是将正确的值传递给转换器(在这种情况下,IsEnabled 的值和 Enabled/Disabled ToolTips 的字符串值)。

如果有人有任何想法,我将非常感激听到他们的声音:)。

提前致谢。

【问题讨论】:

    标签: c# wpf binding dependency-properties converters


    【解决方案1】:

    我设法通过在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 2023-02-19
      相关资源
      最近更新 更多