开发者

WPF - Converter hides dependency on a DependencyProperty

I have a TextBlock (acutally a whole bunch of TextBlocks) where I set the Text to "" if a DependencyProperty in my ViewModel is set to Visiblity.Hidden. I do this via a converter as follows:

<TextBlock Margin="0,0,5,0">
  <TextBlock.Text>
    <Binding Converter="{StaticResource GetVisibilityOfColumnTitles}"
             Path="Name" />
  </TextBlock.Text>
</TextBlock>

The converter looks like this:

public object Convert(object value, Type targetType, 
                      object parameter,System.Globalization.CultureInfo culture)
{
    if (MainMediator.Instance.VisibilityOfWorkItemColumnTitles 
        == Visibility.Visible)
        return value;
    else
        return "";     
}

I admit that this is a bit convoluted way to do this, but I have my reasons (DataContext complications and spacing for the TextBlock)

The problem I have is that when VisibilityOfWorkItemColumnTitles is changed, even though it is a dependency property, TextBlock.Text does not realize there is a dependency there (because it's used in the converter).

Is there a way in the code behind (preferably in the converter) to say, this TextBlock wants to update this binding wh开发者_运维百科en VisibilityOfWorkItemColumnTitles changes?


Since your converter depends on both the Text property from the TextBox and the VisibilityOfWorkItemColumnTitles property on your MainMediator class, you'll probably need to use a MultiBinding and include both properties back in the XAML.

<TextBlock Margin="0,0,5,0"> 
    <TextBlock.Text> 
        <MultiBinding Converter="{StaticResource GetVisibilityOfColumnTitles}">
            <Binding Path="Name" />
            <Binding Path="VisibilityOfWorkItemColumnTitles" Source="{x:Static my:MainMediator.Instance}" />
        </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

(I've used "my" as the XML namespace for your MainMediator class in that code sample.)

Then change your converter to an IMultiValueConverter, and reference values[0] for the text and values1 for the "visibility" property. Now the binding will know if either property changes, and call off to the converter appropriately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜