views:

180

answers:

1

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 when VisibilityOfWorkItemColumnTitles changes?

+2  A: 

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 values[1] for the "visibility" property. Now the binding will know if either property changes, and call off to the converter appropriately.

Matt Hamilton
That is a good idea. The problem is that VisibilityOfWorkItemColumnTitles is in my MainMediator class (the ViewModel) and Name is not (it is an item (sub-item actually) in the listbox). How can I get to the mediator without messing up the datacontext (this is the DataContext complications I alluded to).
Vaccano
Ah, that is what the source is for? I will give it a try. Thanks!
Vaccano
Sweet Answer! Thanks a ton!
Vaccano