0

我想写一个条件控制如下:

<local:ConditionalBlock Condition={Binding CertainBoolValue}>
    <ConditionalBlock.Match>
        <!-- Any content here -->
    </ConditionalBlock.Match>
    <ConditionalBlock.Else>
        <!-- Any content here -->
    </ConditionalBlock.Else>
</local:ConditionalBlock>

目前我不知道这应该如何实施。所以请帮忙。谢谢

编辑

在等待响应时,我使用自定义控件和控件模板实现了自己的解决方案,如下所示:

using System.Windows;
using System.Windows.Controls;

namespace Sample
{
    public class ConditionalControl : ContentControl
    {
        static ConditionalControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ConditionalControl), new FrameworkPropertyMetadata(typeof(ConditionalControl)));
        }

        public object Alternative
        {
            get { return (object)GetValue(AlternativeProperty); }
            set { SetValue(AlternativeProperty, value); }
        }

        public static readonly DependencyProperty AlternativeProperty =
            DependencyProperty.Register("Alternative", typeof(object), typeof(ConditionalControl), new UIPropertyMetadata(null));

        public bool Condition {
            get { return (bool)GetValue(ConditionProperty); }
            set { SetValue(ConditionProperty, value); }
        }

        public static readonly DependencyProperty ConditionProperty =
            DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalControl), new UIPropertyMetadata(null));
    }
}

条件控制.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Sample">

    <Style TargetType="{x:Type local:ConditionalControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ConditionalControl}">
                    <Grid>
                        <ContentPresenter Name="match"/>
                        <ContentPresenter Name="alternative" ContentSource="Alternative" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Condition" Value="True">
                            <Setter TargetName="match" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="alternative" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="Condition" Value="False">
                            <Setter TargetName="match" Property="Visibility" Value="Collapsed"/>
                            <Setter TargetName="alternative" Property="Visibility" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

示例用法:

<Grid>
    <local:ConditionalControl Condition="{Binding CertainBoolValue}">
        <Label>Match case</Label>
        <local:ConditionalControl.Alternative>
            <Label>Alternative case</Label>
        </local:ConditionalControl.Alternative>
    </local:ConditionalControl>
</Grid>

无论如何,感谢卢克伍德沃德的及时回复。我会将您的答案标记为已接受

4

1 回答 1

1

一种方法是使用以下 XAML 创建一个 UserControl:

<ContentControl x:Class="WpfApplication1.ConditionalBlock"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />

以及以下代码隐藏:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class ConditionalBlock : ContentControl
    {
        public static readonly DependencyProperty ConditionProperty =
            DependencyProperty.Register("Condition", typeof(bool), typeof(ConditionalBlock),
                                        new FrameworkPropertyMetadata(Condition_Changed));

        public ConditionalBlock()
        {
            InitializeComponent();
            Loaded += ((s, e) => UpdateContent());
        }

        public bool Condition
        {
            get { return (bool)GetValue(ConditionProperty); }
            set { SetValue(ConditionProperty, value); }
        }

        public object Match { get; set; }

        public object Else { get; set; }

        private static void Condition_Changed(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var conditionalBlock = obj as ConditionalBlock;
            if (conditionalBlock != null)
            {
                conditionalBlock.UpdateContent();
            }
        }

        private void UpdateContent()
        {
            Content = (Condition) ? Match : Else;
        }
    }
}

请注意,我们已将超类从 更改UserControlContentControl

这使用了条件的依赖属性,以便我们在绑定值更改时得到通知。当条件改变时,我们通过分配给Content我们继承自的依赖属性来更新哪些内容是可见的ContentControl。最后,为了确保在第一次显示控件时出现一个或其他内容,我们更新Loaded事件处理程序中的内容。(从构造函数中调用它不起作用,因为此时尚未设置Matchand属性。)Else

我假设MatchandElse属性的内容不会改变,所以我没有费心将它们实现为依赖属性。

于 2012-10-21T17:38:22.037 回答