GolfARama
GolfARama

Reputation: 807

How do I properly wire up visibility binding on a grid using Silverlight MVVM pattern?

More specifically, am I doing it right because it is not working. I have a bool property in my ViewModel along with a text property for a TextBlock. If I change the text property, the results appear on screen immediately. So I know something is listening for the property changes. The visibility property is set to use a bool-to-visibility converter but that converter never gets called. I'm sure it is just some part of the data binding that I am not doing right but I have tried everything suggested on StackOverflow as well as setting the binding manually and several other things. I have over 12 hours in this problem and am feeling really let down by the whole Silverlight / MVVM architecture in general. And I was so excited that I "figured it out", too.

Particulars: Silverlight 5.1.10144

App.xaml resources: <Application.Resources> <vidstreams:ManagementViewModel x:Key="managementViewModel"/> <vidstreams:VisibilityConverter x:Key="visConverter"/> </Application.Resources>

MyView.xaml DataContext:

<UserControl.DataContext>
    <Binding Source="{StaticResource managementViewModel}"/>
</UserControl.DataContext>

MyView.xaml Grid visibility binding:

<Grid x:Name="LayoutRoot" Background="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="60"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid ...
             Visibility="{Binding IsWaitingVisible, Mode=OneWay, Converter={StaticResource visConverter}}">...</Grid>
    <Button x:Name="test"
         Click="test_Click"
         Content="test visibility"
         HorizontalAlignment="Left"
         Margin="0,0,0,0"
             Grid.Row="1"
             VerticalAlignment="Top"/>
</Grid>

MyView.xaml.cs Instance property and test_Click code:

public ManagementViewModel DataContextObject
        {
            get
            {
                return (ManagementViewModel)App.Current.Resources["managementViewModel"];
            }
        }

    protected void test_Click(object sender, RoutedEventArgs e)
        {

            DataContextObject.IsWaitingVisibile = !DataContextObject.IsWaitingVisibile; //doesn't toggle the visibility or cause the converter to be hit
            DataContextObject.WaitingText = "Loading data..."; //works
        }

ManagementViewModel class innards:

public class ManagementViewModel : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  protected void OnPropertyChanged(string propertyName)
  {
    var p = PropertyChanged;
    if (p != null)
    {
        p(this, new PropertyChangedEventArgs(propertyName));
    }
  }

  bool mIsWaitingVisible = true;
  public bool IsWaitingVisibile 
  {
    get { return mIsWaitingVisible; }
    set
    {
        mIsWaitingVisible = value;
        OnPropertyChanged("IsWaitingVisible");
    }
  }
  ...
}

I would post the converter code here but it isn't even being hit. It's a simple converter like the others found in various posts on this site, anyway.

Any thoughts or suggestions - or just confirmation that this is some sort of regression bug in 5 maybe? - would be so appreciated. Perhaps the visibility binding instructions have to be set differently. Remember, the TextBlock works fine:

<TextBlock x:Name="WaitingTextBlock"
           Text="{Binding WaitingText}" .../>

Upvotes: 0

Views: 912

Answers (1)

Hitesh Patel
Hitesh Patel

Reputation: 449

@GolfARama

Hi can you try with this

Visibility="{Binding IsWaitingVisible, Mode=TwoWay, Converter={StaticResource visConverter}}">

Upvotes: 1

Related Questions