在 WPF 中绑定 ContentPresenter 的可见性时遇到问题

发布于 2024-09-17 09:59:05 字数 2408 浏览 7 评论 0原文

我有以下 XAML:

<UserControl.Resources>

<DataTemplate x:Key="ExpanderTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
        <Rectangle Stroke="Black" StrokeThickness="3" Width="2" Height="6" Fill="Black" />
    </Grid>
</DataTemplate>

<DataTemplate x:Key="CollapserTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
    </Grid>
</DataTemplate>

</UserControl.Resources>

<Grid>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=Name}" />
    <Grid>
        <ContentPresenter x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
        <ContentPresenter x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />
    </Grid>
</StackPanel>
</Grid>

如您所见,它本质上是一个文本块,其内容绑定到名称,以及两个内容呈现器,其可见性绑定到源类上的几个 Visibility 对象。源类如下所示:

public class MyViewModel
{
public string Name { get; set; }

public Visibility CollapserVisibility
{
    get
    {
        if (IsExpandable && IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public Visibility ExpanderVisibility
{
    get
    {
        if (IsExpandable && !IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public bool IsExpanded { get; set; }
public bool IsExpandable { get; set; }
}

我遇到的问题是与 Visibility 对象的绑定甚至从未发生。与名称字符串的绑定确实发生,并且我已经验证了这一点(通过视觉和扩展并在 getter 中放置断点),但是当我在 CollapserVisibility 和 ExpanderVisibility 的 getter 中放置断点时对象,这些断点甚至永远不会被击中。这是为什么呢?我在 Visual Studio 的输出窗口中也没有收到任何绑定错误,这让我更加困惑,因此它的行为就像绑定已正确设置一样。

我是否不允许绑定 ContentPresenter 的 Visibility 属性?我还尝试将可见性绑定移至数据模板(在 XAML 中)中的“网格”对象上,但无济于事......它不起作用。

我的绑定有什么问题吗?感谢您的任何帮助。

I have the following XAML:

<UserControl.Resources>

<DataTemplate x:Key="ExpanderTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
        <Rectangle Stroke="Black" StrokeThickness="3" Width="2" Height="6" Fill="Black" />
    </Grid>
</DataTemplate>

<DataTemplate x:Key="CollapserTemplate">
    <Grid>
        <Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="10" Fill="White" />
        <Rectangle Stroke="Black" StrokeThickness="1" Width="6" Height="1" Fill="Black" />
    </Grid>
</DataTemplate>

</UserControl.Resources>

<Grid>
<StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Path=Name}" />
    <Grid>
        <ContentPresenter x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
        <ContentPresenter x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />
    </Grid>
</StackPanel>
</Grid>

As you can see, it's essentially a textblock with its contents bound to a name, and two contentpresenters which have their visibility bound to a couple of Visibility objects on the source class. The source class looks like this:

public class MyViewModel
{
public string Name { get; set; }

public Visibility CollapserVisibility
{
    get
    {
        if (IsExpandable && IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public Visibility ExpanderVisibility
{
    get
    {
        if (IsExpandable && !IsExpanded)
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Collapsed;
        }
    }
}

public bool IsExpanded { get; set; }
public bool IsExpandable { get; set; }
}

The problem I am experiencing is that the bindings with the Visibility objects never even happen. The binding with the Name string does happen, and I have verified that (both visually and by expanding out and putting a breakpoint in the getter), but when I put breakpoints in the getters of the CollapserVisibility and ExpanderVisibility objects, those breakpoints never even get hit. Why is this? I'm also not getting any binding errors in the output window of Visual Studio, which further confuses me, so it acts as if the binding was set up correctly.

Am I not allowed to bind the Visibility property of a ContentPresenter? I also tried moving the Visibility bindings to be on the "Grid" objects in the data templates (in the XAML), but to no avail...it didn't work.

What's wrong with my binding? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟的代价 2024-09-24 09:59:05

如果您将 ContentPresenter 更改为 ContentControl,它就会起作用(因为它绑定到您的可见性属性 - 我忽略了您的虚拟机不通知更改的事实) :

<ContentControl x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
<ContentControl x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />

除了 ContentPresenter 应该在 ContentControl 模板中使用这一事实之外,我真的无法解释为什么会这样。但是,对我来说,它应该仍然有效。我认为需要一些基于反射器的侦查才能解决这个问题。

If you change the ContentPresenter to a ContentControl it works (in that it binds to your visibility properties - I'm ignoring the fact that your VM doesn't notify of changes):

<ContentControl x:Name="ExpanderPresenter" ContentTemplate="{StaticResource ExpanderTemplate}" Visibility="{Binding ExpanderVisibility}" />
<ContentControl x:Name="CollapserPresenter" ContentTemplate="{StaticResource CollapserTemplate}" Visibility="{Binding CollapserVisibility}" />

I really can't explain why this is, apart from the fact that ContentPresenter is supposed to be used within the template of a ContentControl. But, to me, it should still work. I think it would require some Reflector-based sleuthing to figure this one out.

我爱人 2024-09-24 09:59:05

您是否曾将任何内容放入内容演示器中?它可能没有显示任何内容,因为没有什么可显示的。数据模板规定了数据的外观,但您需要首先将数据实际放入其中。

除此之外,绑定看起来应该适用于初始值,但如果 IsExpandable 或 IsExpanded 属性发生更改,它们不会更新。

使用 MultiDataTrigger 来控制可见性可能会更好。

Are you ever putting any content into the contentpresenter? It might not be showing anything because there's nothing to show. The datatemplate dictates how the data should look, but you need to actually put data into it first.

Aside from that, the bindings look like they should work for the initial values, but they won't update if the IsExpandable or IsExpanded properties change.

You'd probably be a lot better off using a MultiDataTrigger to control the visibility.

梦回旧景 2024-09-24 09:59:05

显然这不是您的答案,但其他人可能会发现这很有用。

如果您绑定到 bool,请不要忘记将 bool 包含到可见性转换器。

..., Converter={StaticResource BoolToVis}}"

Obviously this isn't your answer, but other people might find this useful.

If you bind to a bool, don't forget to include the bool to visibility converter.

..., Converter={StaticResource BoolToVis}}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文