C# 单个自定义ControlTemplate的不同绑定
我已经定义了这个控制模板 在同一页中多次。每次将示例中的文本绑定到不同的文本时,复选框IsChecked将是对布尔属性的双向绑定 我怎样才能把它绑好C# 单个自定义ControlTemplate的不同绑定,c#,uwp,C#,Uwp,我已经定义了这个控制模板 在同一页中多次。每次将示例中的文本绑定到不同的文本时,复选框IsChecked将是对布尔属性的双向绑定 我怎样才能把它绑好 public sealed partial class MainPage : Page { public Status Status { get; set; } = new Status(); public MainPage() { InitializeComponen
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; }
}
由于您只使用常规复选框已有的属性,我建议您为复选框编写一个ControlTemplate: 您可以这样简单地使用它们: 如果您有越来越多的属性,您当然可以编写自己的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();
}
}
}
或者甚至是一个自定义控件,具有默认样式和模板,但这要高级一点。您可以直接将它绑定到datacontext{Binding Path=.}并交换掉datacontext。或者编写您自己的UserControl,它具有类似ListBox的DisplayMemberName的属性。我对整个页面使用单个DataContext,并且这些控件将绑定到DataContext的不同属性。我应该绑定什么,ControlTemplate还是ContentControl?取决于您如何使用它。你能给我一个答案吗?我还没有呢?我还是编辑了它。真的不知道还有什么好补充的。哦,那很简单。使用自定义控件应该很容易,我会在一分钟后发布,否则soChecked当然应该被选中,这是一个打字错误。