网格内的ContentPresenter可见性绑定不起作用? [英] ContentPresenter Visibility binding inside Grid not working?

查看:256
本文介绍了网格内的ContentPresenter可见性绑定不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网格:

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

这两个 ContentPresenters 具有相同的 Grid.Row 的定义是因为一次只能看到其中一个。
我有以下 boolToVisibility 转换器:

Those two ContentPresenters has the same Grid.Row definded because only one of them should be visible at once. I have following boolToVisibility converter:

[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;
    }
}

有一个问题:两者 ContentPresenters 可见!我还注意到,应用程序仅读取 ShowPredictiveWorkspace 属性。从未调用在 ShowStandardWorkspace getter上设置的断点。
我猜这是一个愚蠢的错误,但我确实找不到。

And there's the problem: both ContentPresenters are visible! I noticed also that only ShowPredictiveWorkspace property is being read by a app. Breakpoint set on ShowStandardWorkspace getter is never called. I guess it some stupid mistake but I really can't find it.

编辑:

public bool ShowStandardWorkspace
    {
        get { return this._showStandardWorkspace; }
        set
        {
            this._showStandardWorkspace = value;
            this.OnPropertyChanged(() => this.ShowStandardWorkspace);
        }
    }


推荐答案

此是因为将可见性与 ContentPresenter 元素上的转换器绑定无效。

This is because it does not work to bind visibility with a converter on the ContentPresenter element.

如果更改 ContentPresenter ContentControl ,它将可以将可见性属性与转换器绑定,这样就不必嵌套

If you change the ContentPresenter to a ContentControl it will work to bind the visibility property with a converter, and then you don't have to nest it within another element.

这显然是因为 ContentPresenter 是一种轻量级元素,应在以下元素中使用一个 ControlTemplate

This is apparently because ContentPresenter is a light weight element that is meant to be used within a ControlTemplate.

从MSDN (突出显示):


您通常使用
ContentControl的ControlTemplate中的ContentPresenter
来指定要在何处添加内容。每个
ContentControl类型在其默认
ControlTemplate中都有一个ContentPresenter。

You typically use the ContentPresenter in the ControlTemplate of a ContentControl to specify where the content is to be added. Every ContentControl type has a ContentPresenter in its default ControlTemplate.

当ContentPresenter对象位于ContentControl的
ControlTemplate中时,Content,ContentTemplate和
ContentTemplateSelector属性将从$ b中获取其值。具有ContentControl相同名称的$ b属性。您可以通过设置ContentSource
属性或将其绑定来使
ContentPresenter属性从模板化父级的
个其他属性中获取这些属性的值。

When a ContentPresenter object is in a ControlTemplate of a ContentControl, the Content, ContentTemplate, and ContentTemplateSelector properties get their values from the properties of the same names of the ContentControl. You can have the ContentPresenter property get the values of these properties from other properties of the templated parent by setting the ContentSource property or binding to them.

这篇关于网格内的ContentPresenter可见性绑定不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆