繁体   English   中英

通过MultiBinding将错误的值传递给MultiValueConverter

[英]Passing incorrect values into MultiValueConverter by MultiBinding

我一直在尝试为RadioButton实现动态工具提示(WPF)(当RadioButton的IsEnabled更改时,工具提示会切换)。 我想用MultiValueConverter来实现这一点,MultiValueConverter有点像一个普通的Converter,它接受3个值-IsEnabled值,启用的ToolTip和禁用的ToolTip以此顺序排列。

但是可悲的是我遇到了一个问题,我还无法解决。 基本上,当代码到达Convert方法时,值数组将填充DependencyProperty.UnsetValue项目。

我在谷歌搜索时设法找到的问题是,问题可能是由错误的DataContext引起的,如此处提到的。Converter中的WPF MultiBinding失败==> 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>

因此,我希望从中得到的结果是正确的值将传递到Converter(在这种情况下,为IsEnabled的值和Enabled / Disabled ToolTips的字符串值)。

如果有人有任何想法,我将不胜感激:)。

提前致谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM