UserControl DataTemplate类型的依赖项属性的默认值

时间:2014-09-14 10:00:27

标签: wpf user-controls dependency-properties default-value

我有一个简单的EntriesViewModel,它存储项目列表以及支持添加/删除命令。 (添加/删除命令左侧)

public class EntriesViewModel
{
    public ObservableCollection<string> Entries { get; private set; }

    public EntriesViewModel()
    {
        Entries = new ObservableCollection<string>() { "One", "Two", "Three" };
    }
}

要查看此内容,我创建了一个简单的EntriesEditor usercontrol,它使用列表框显示添加/删除的条目和按钮(添加/删除命令省略)

 public partial class EntriesEditor : UserControl
{
    public EntriesEditor()
    {
        InitializeComponent();
    }
}

<UserControl x:Class="UserControlDefaults.EntriesEditor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:l="clr-namespace:UserControlDefaults"
         VerticalAlignment="Top">
<DockPanel Height="200">
    <UniformGrid Columns="2"
                 DockPanel.Dock="Bottom">
        <Button Content="Add" />
        <Button Content="Remove" />
    </UniformGrid>
    <ListBox ItemsSource="{Binding Entries}"
             ItemTemplate="{Binding EntryTemplate, RelativeSource={RelativeSource AncestorType={x:Type l:EntriesEditor}}}"/>
</DockPanel>

并且如此使用:

<l:EntriesEditor DataContext="{Binding EntriesViewModel}"/>

我现在希望EntriesEditor支持定义自定义ItemTemplate的功能。所以我添加了一个EntryTemplate属性。

public partial class EntriesEditor : UserControl
{
    public DataTemplate EntryTemplate
    {
        get { return (DataTemplate)GetValue(EntryTemplateProperty); }
        set { SetValue(EntryTemplateProperty, value); }
    }

    public static readonly DependencyProperty EntryTemplateProperty =
        DependencyProperty.Register("EntryTemplate", typeof(DataTemplate), typeof(EntriesEditor), new PropertyMetadata(null));

    public EntriesEditor()
    {
        InitializeComponent();
    }
}

<UserControl x:Class="UserControlDefaults.EntriesEditor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:l="clr-namespace:UserControlDefaults"
         VerticalAlignment="Top">
<DockPanel Height="200">
    <UniformGrid Columns="2"
                 DockPanel.Dock="Bottom">
        <Button Content="Add" />
        <Button Content="Remove" />
    </UniformGrid>
    <ListBox ItemsSource="{Binding Entries}"
             ItemTemplate="{Binding EntryTemplate, RelativeSource={RelativeSource AncestorType={x:Type l:EntriesEditor}}}"/>
</DockPanel>

使用的是:

<l:EntriesEditor DataContext="{Binding EntriesViewModel}">
    <l:EntriesEditor.EntryTemplate>
        <DataTemplate>
            <Border BorderThickness="1"
                    BorderBrush="Green"
                    Padding="5">
                <ContentPresenter Content="{Binding}"/>
            </Border>
        </DataTemplate>
    </l:EntriesEditor.EntryTemplate>
</l:EntriesEditor>

但是现在我想在EntriesEditor控件中定义一个默认的EntryTemplate,我该怎么做?

我能想到的唯一方法就是这样:

<UserControl x:Class="UserControlDefaults.EntriesEditor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:l="clr-namespace:UserControlDefaults"
         VerticalAlignment="Top">

<UserControl.Style>
    <Style TargetType="{x:Type l:EntriesEditor}">
        <Setter Property="EntryTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderThickness="2"
                            BorderBrush="Red">
                        <ContentPresenter Content="{Binding}" />
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Style>

<DockPanel Height="200">
    <UniformGrid Columns="2"
                 DockPanel.Dock="Bottom">
        <Button Content="Add" />
        <Button Content="Remove" />
    </UniformGrid>
    <ListBox ItemsSource="{Binding Entries}"
             ItemTemplate="{Binding EntryTemplate, RelativeSource={RelativeSource AncestorType={x:Type l:EntriesEditor}}}"/>
</DockPanel>

但这看起来并不合适。

0 个答案:

没有答案