在自定义Silverlight UserControl中创建DependencyPropety

时间:2013-01-21 17:33:09

标签: silverlight mvvm dependency-properties

任何人都可以帮我在我的自定义Silverlight UserControl中创建ItemsSource属性吗?

这是一个非常简单的ViewModel:

public class MyVM
{
    public ObservableCollection<int> Values { set; get; }

    public MyVM()
    {
        this.Values = new ObservableCollection<int>();
    }
}

这是我的(内部)UserControl,我进入主UserContorl(MainPage):

<UserControl x:Class="SilverlightApplication1.SilverlightControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White" >
    <Border BorderBrush="Black" BorderThickness="2">
        <ListBox Margin="5" Name="lst" />
    </Border>
</Grid>
</UserControl>

public partial class SilverlightControl1 : UserControl
{
    public IEnumerable MyItemsSource
    {
        get
        {
            return (IEnumerable)GetValue(MyItemsSourceProperty);
        }
        set
        {
            SetValue(MyItemsSourceProperty, value);
        }
    }
    public static readonly DependencyProperty MyItemsSourceProperty =
        DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(SilverlightControl1), new PropertyMetadata(null));


    public SilverlightControl1()
    {
        InitializeComponent();
    }
}

这是一个托管我的UserControl的小型容器:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <local:SilverlightControl1 Name="qqq" MyItemsSource="{Binding Path=Values}"/>

</Grid>
</UserControl>

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        MyVM vm = new MyVM();
        vm.Values.Add(1);
        vm.Values.Add(2);
        vm.Values.Add(3);
        vm.Values.Add(4);

        this.DataContext = vm;
    }
}

如何将数据绑定到我的内部ListBox?

2 个答案:

答案 0 :(得分:0)

首先,您的SilverlightControl1没有自己的DataContext,这意味着它继承了其容器的DataContext(在本例中为MainPage)。我会回答假设你想要这样设置(而不是SilverlightControl1有自己的DC)。有了这个说,MyItemsSource是没用的。你可以放在一起。将其从MainPage.xaml中删除,只需将控件包含在内:

<Grid x:Name="LayoutRoot" Background="White">
    <local:SilverlightControl1 x:Name="qqq" />
</Grid>

其次,您的ListBox没有绑定任何东西。由于它继承了Main的DC,因此可以绑定到Values

<Grid x:Name="LayoutRoot" Background="White" >
    <Border BorderBrush="Black" BorderThickness="2">
        <ListBox Margin="5" Name="lst" ItemsSource="{Binding Values}" />
    </Border>
</Grid>

答案 1 :(得分:0)

我已完全停止使用UserControl,部分原因是这样。我改用CustomControls。可用性和抱怨完全相同。你有代码隐藏的可能性和一个单独的模型(虽然我主要使用控件的代码作为模型本身设置this.DataContext = this;)。我的代码也变得更加结构化(至少我是这么认为),我有更多的可能性来改变控件的UI。

使用CustomControls的唯一缺点是我没有像UserControls和Windows那样的设计界面。 UI被放入Generic.xaml文件中(默认)并通过在编辑器中编写XAML来构建,而不是使用鼠标。这开始是个糟糕的事,但我已经习以为常了。

所以我的答案是,如果您使用CustomControls,您可以完全相同。 Bindings的写法如下:

MyItemsSource="{Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path=Values}"

......或简单地说:

MyItemsSource="{TemplateBinding Values}"

...如果您不必在Binding中添加任何转换器或其他任何内容。