网格内的 ContentPresenter 可见性绑定不起作用? [英] ContentPresenter Visibility binding inside Grid not working?
问题描述
我有以下网格:
<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(我的突出显示):
您通常在 ControlTemplate 中使用 ContentPresenterContentControl 指定要添加内容的位置.每一个ContentControl 类型默认有一个 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 属性从ContentControl 的同名属性.你可以拥有ContentPresenter 属性从中获取这些属性的值通过设置 ContentSource 模板化父级的其他属性属性或绑定到它们.
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屋!