0

ItemsSourceカスタム Silverlight UserControl でプロパティを作成するのを手伝ってくれる人はいますか?

これが私の非常に単純なViewModelです:

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

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

これは、メインの UserContorl (MainPage) に貼り付けた (内部の) UserControl です。

<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 にデータをバインドするにはどうすればよいですか?

4

2 に答える 2

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>
于 2013-01-21T20:25:19.547 に答える
0

このため、UserControl の使用を完全に停止しました。代わりに CustomControls を使用します。使いやすさと複雑さはまったく同じです。コードビハインドと別のモデルの可能性があります(ただし、ほとんどの場合、コントロールのコードをモデル自体の設定として使用しますthis.DataContext = this;)。コードもより構造化され (少なくとも私はそう思います)、コントロールの UI を変更する可能性が高くなります。

CustomControls を使用する場合の唯一の欠点は、UserControls や Windows のようなデザイン サーフェイスがないことです。UI は代わりに Generic.xaml ファイルに配置され (既定)、マウスを使用するのではなく、エディターで XAML を記述することによって構築されます。それは最初はがっかりしましたが、驚くほど早く慣れました。

私の答えは、CustomControls を使用すれば、まったく同じことができるということです。バインディングは次のように記述します。

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

...または単に:

MyItemsSource="{TemplateBinding Values}"

...コンバーターやその他のものをバインディングに追加する必要がない場合。

于 2013-01-21T22:38:34.963 に答える