ContentPresenter Binding Visibility Inside Grid Not Working?

I have the following grid:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    ...
    <ContentPresenter Grid.Row="1" Content="{Binding Path=PredictiveWorkspace}"
                      Visibility="{Binding Path=ShowPredictiveWorkspace, 
                      Converter={StaticResource boolToVisibility}}"/>
    <ContentPresenter Grid.Row="1" Content="{Binding Path=M2Workspace}"
                      Visibility="{Binding Path=ShowStandardWorkspace, 
                      Converter={StaticResource boolToVisibility}}"/>
    ...
</Grid>

Those two ContentPresentershave the same Grid.Row, because only one of them should be visible immediately. I have the following converter boolToVisibility:

[ValueConversion(typeof(bool), typeof(System.Windows.Visibility))]
public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
        {
            return System.Windows.Visibility.Visible;
        }
        else
            return System.Windows.Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

And there is a problem: both ContentPresentersare visible! I also noticed that only the application ShowPredictiveWorkspaceis read by the application. A breakpoint set to ShowStandardWorkspaceis not called. I think this is some kind of stupid mistake, but I really can't find it.

EDIT:

public bool ShowStandardWorkspace
    {
        get { return this._showStandardWorkspace; }
        set
        {
            this._showStandardWorkspace = value;
            this.OnPropertyChanged(() => this.ShowStandardWorkspace);
        }
    }
+4
source share
4 answers

, ContentPresenter.

ContentPresenter ContentControl, , , .

, -, , ContentPresenter - , ControlTemplate.

MSDN ( ):

ContentPresenter ControlTemplate ContentControl, , . ContentControl ContentPresenter ControlTemplate.

ContentPresenter ControlTemplate ContentControl, Content, ContentTemplate ContentTemplateSelector ContentControl. ContentPresenter , ContentSource .

+9

, , , contentpresenter. - ViewModel, ContentPresenter, , - , boolToVisibilityConverter.

- ContentPresenter a Grid ( , ) Visibility Grid . .

+3

AncestorType. DataContext , ContentPresenter, Visual Tree, . :

Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ShowStandardWorkspace}"

Grid , DataContext. , .. , AncestorLevel int. , .

+1

:

  • ShowStandardWorkspace
  • OnPropertyChanged ( "ShowStandardWorkspace" ),
  • The ShowStandardWorkspace property is simply not set to false
  • there may be an invalid DataContext for the second ContentPresenter
0
source

All Articles