wpf UserControl in several binding methods

Original: WPF UserControl in several binding methods

We often extract some reusable controls, whether a property needs to be reused directly determine the binding mode of this property.

1, completely non-reusable controls

There are some business related controls and strong, their properties entirely from ViewModel, relatively more complex controls, the easier this way. such as:

// ChooseUc.xaml
<UserControl>
    <StackPanel Orientation="Horizontal">
        <Label Content="选择一个水果: "/>
        <ComboBox ItemsSource="{Binding Fruits}"/>
    </StackPanel>
</UserControl>

When used directly <my:ChooseUc />to bind directly to ViewModel inside List<Fruit> Fruits, do not do extra work. Benefit is particularly convenient, the price is fully coupled with vm.

2, fully reusable controls

Similar controls and more, will be able to take some completely reusable controls, these controls has nothing to do with the business. In order to achieve this reuse, to do:

  1. Out of all variable attributes defined in the internal controls
  2. These properties are defined in the bound control
  3. When used externally, and then bind these properties and vm

details as follows:

// ChooseUc.xaml
<UserControl>
    <StackPanel x:Name="root" Orientation="Horizontal">
        <Label Content="{Binding Header}"/>
        <ComboBox ItemsSource="{Binding Items}"/>
    </StackPanel>
</UserControl>

Cs corresponding code:

// ChooseUc.xaml.cs
public ChooseUc() {
    InitializeComponent();
    root.DataContext = this; // 这句很关键!
}

public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IEnumerable), typeof(ChooseUc));
public IEnumerable Items {
    get { return (IEnumerable)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value); }
}

//HeaderProperty类似,此处省略

When used as follows:

<my:ChooseUc Header="{Binding FruitHeader}" Items="{Binding Fruits}" />

Special attention root.DataContext = this;, this one the internal controls from the outside world, and in fact could not see inside the ViewModel , only to see the properties internally defined (Header, Items and the like). Any scene some trouble, but the benefits can be applied to a label +1 drop-down box, high reusability.

The disadvantage is that, in a custom control cascade, which is just a nightmare. Imagine three UserControl: C> B> A (C containing B, B comprises A). Only a corresponding C vm, B in this case to include all the attributes of A. The fact is, B to include all the property of all child controls, this is maddening!

Fortunately, in most cases we do not need cascade nested custom control, but we can also give up some reusability, in order to facilitate the obtaining of default binding vm.

3, part of reusable controls

Another type of case is part of a reusable control, consider a scenario: The user can select two fruits, then Fruitscan be tied directly to the vm years, but SelectedFruitneed to bind respectively:

// ChooseUc.xaml
<UserControl x:Name="uc">
    <StackPanel Orientation="Horizontal">
        <Label Content="选择一个水果: "/>
        <ComboBox ItemsSource="{Binding Fruits}" SelectedItem="{Binding SelectedFruit, ElementName=uc}"/>
    </StackPanel>
</UserControl>

// 类似的,ChooseUc.xaml.cs里定义SelectedFruit依赖属性

When used as follows:

<StackPanel>
    <my:ChooseUc SelectedFruit="{Binding Fruit1}" />
    <my:ChooseUc SelectedFruit="{Binding Fruit2}" />
</StackPanel>

In this case, even the cascade, Fruitsno need to repeat the definition was given SelectedFruitflexibility. The key here is: only the need to reuse the property, bound to the internal controls, like other property inheritance vm .

4. Summary

The actual development process in most cases is 1 + 3, + that is completely reuse part of reusable controls. Even in the face completely reusable controls, not the General Assembly to form a cascade.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12329518.html