简体   繁体   English

如何为WPF属性制作模板?

[英]How can I make template for WPF properties?

On my WPF XAML form I've got a lot of elements which I bind to my properties. 在WPF XAML表单上,我有很多绑定到属性的元素。

For each one property I'm making this steps: 对于每个属性,我正在执行以下步骤:

<TextBlock Grid.Column="0" Grid.Row="0" Name="TB11" Text="{Binding TBX11}" 
            VerticalAlignment="Center" HorizontalAlignment="Center" 
            DataContext="{Binding RelativeSource={RelativeSource Self}}" />

and

#region TBX11
private static void OnXBPropertyChanged(DependencyObject dependencyObject,
                DependencyPropertyChangedEventArgs e) 
{
    table myUserControl = dependencyObject as table;
    myUserControl.OnPropertyChanged("XB11");
    myUserControl.OnCaptionPropertyChanged(e);
}
private void OnCaptionPropertyChanged(DependencyPropertyChangedEventArgs e) {
    TB11.Text = TBX11;
}
public static readonly DependencyProperty TBX11Property =
        DependencyProperty.Register("TBX11", typeof(string), typeof(table),
        new PropertyMetadata(string.Empty, OnXBPropertyChanged));
public string TBX11 {
    get { return GetValue(TBX11Property).ToString(); }
    set {
        SetValue(TBX11Property, value);
        OnPropertyChanged("TBX11");
    }
}
#endregion

I can't even realize for now how many times I will need to write the same here but I don't know if I can make it somehow easier? 我什至无法意识到我现在需要在这里写多少遍,但是我不知道我是否可以使它更容易些? Because all I need to set from here is WPF block name and binding name. 因为我需要在此处设置的只是WPF块名称和绑定名称。

There are a couple of ways you can reduce repetition: 有两种方法可以减少重复:

  1. Introduce user-controls for reducing repeated XAML and C# code. 引入用于减少重复XAML和C#代码的用户控件。 See this tutorial I wrote for more details. 有关更多详细信息,请参见我编写的本教程
  2. If you have a list of identical UI elements, as an ItemsControl with an ItemTemplate to create the repeated elements. 如果您具有相同的UI元素列表,则将其与具有ItemTemplateItemsControl一起创建重复的元素。

I have spotted a few issues with your code: 我发现您的代码存在一些问题:

  1. In your OnCaptionPropertyChanged method you set the TextBlock.Text , you should noty do this - this binding will take care of the update! OnCaptionPropertyChanged方法中,设置TextBlock.Text ,您不应该这样做-该绑定将负责更新!
  2. Within your TBX11 property you invoke this method OnPropertyChanged("TBX11") , you should not add any logic to the getters or setters of dependency properties. TBX11属性中,调用此方法OnPropertyChanged("TBX11") ,不应将任何逻辑添加到依赖项属性的获取器或设置器中。 There is no guarantee these will be invoked. 不能保证会调用它们。
  3. Your TextBlock.DataContext is set to itself, so the Text binding will not work! 您的TextBlock.DataContext设置为自身,因此Text绑定将不起作用!

I don't know if I can make it somehow easier? 我不知道是否可以使它更容易些?

You can use MVVM framework like Caliburn Micro . 您可以使用Calicali Micro之类的MVVM框架。

Caliburn will allow you to rewrite your XAML as Caliburn将允许您将XAML重写为

 <TextBlock Grid.Column="0" Grid.Row="0" Name="TB11"  
        VerticalAlignment="Center" HorizontalAlignment="Center"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM