简体   繁体   中英

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). 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.

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.

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.

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).

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. Though I don't understand why it did not work with the RelativeSources, it works. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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