簡體   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