单个自定义ControlTemplate的不同绑定

时间:2018-03-01 13:12:16

标签: c# uwp

我已经定义了这个ControlTemplate

<Page>
    <Page.Resources>
        <ControlTemplate x:Key="Test">
            <StackPanel>
                <TextBlock Text="Example"></TextBlock>
                <CheckBox  IsChecked="True"></CheckBox>
            </StackPanel>
        </ControlTemplate>
    </Page.Resources>
    <Grid>
        <ContentControl Template="{StaticResource Test}"> </ContentControl><!-- Text here should be Status.AlertText and IsChecked should be two-way binding for Status.AlertChecked -->
        <ContentControl Template="{StaticResource Test}"></ContentControl> <!-- Text here should be Status.FocusText and IsChecked should be two-way binding for Status.FocusChecked -->
    </Grid>
</Page>

在同一页中多次。每次“示例”中的文本将绑定到不同的文本,并且复选框“IsChecked”将是对布尔属性的双向绑定。

如何正确绑定?

public sealed partial class MainPage : Page
    {
        public Status Status { get; set; } = new Status();
        public MainPage()
        {
            InitializeComponent();
            DataContext = Reporte;
        }
    }

public class Reporte :INotifyPropertyChanged
{
    public bool FocusChecked { get; set; }
    public string FocusText { get; set; }
    public bool AlertChecked { get; set; }
    public string AlertText  { get; set; }
}

1 个答案:

答案 0 :(得分:1)

由于您只使用常规CheckBox已有的属性,我建议您为CheckBoxes编写一个ControlTemplate:

<ControlTemplate x:Key="VerticalCheckBox" TargetType="CheckBox">
    <StackPanel>
        <ContentControl Content="{TemplateBinding Content}"/>
        <CheckBox IsChecked="{TemplateBinding IsChecked}"/>
    </StackPanel>
</ControlTemplate>

你可以这样简单地使用它们:

<CheckBox IsChecked="{Binding FocusChecked}" Content="{Binding FocusText}" Template="{StaticResource VerticalCheckBox}"/>
<CheckBox IsChecked="{Binding AlertChecked}" Content="{Binding AlertText}" Template="{StaticResource VerticalCheckBox}"/>

如果您拥有越来越多的属性,您当然可以编写自己的UserControl,例如像这样:

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

namespace WpfApp7
{
    public partial class MyUserControl : UserControl
    {
        public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(
            "IsChecked", typeof(bool?), typeof(MyUserControl), new PropertyMetadata(default(bool?)));

        public bool? IsChecked
        {
            get { return (bool?) GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text", typeof(string), typeof(MyUserControl), new PropertyMetadata(default(string)));

        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public MyUserControl()
        {
            InitializeComponent();
        }
    }
}
<UserControl x:Class="WpfApp7.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApp7"
             mc:Ignorable="d">
    <StackPanel>
        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyUserControl}}, Path=Text}"/>
        <CheckBox IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyUserControl}}, Path=IsChecked}"/>
    </StackPanel>
</UserControl>

甚至是自定义Control,使用默认样式和模板,但更高级。