【问题标题】:Creating DependencyPropety in custom Silverlight UserControl在自定义 Silverlight UserControl 中创建 DependencyPropety
【发布时间】:2013-01-04 19:57:36
【问题描述】:

谁能帮我在我的自定义 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?

【问题讨论】:

    标签: silverlight mvvm dependency-properties


    【解决方案1】:

    首先,您的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>
    

    【讨论】:

      【解决方案2】:

      我已经完全停止使用 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}"
      

      ...如果您不必将任何转换器或其他任何东西添加到绑定中。

      【讨论】:

        猜你喜欢
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        • 2011-02-16
        相关资源
        最近更新 更多