2

コレクションプロパティを使用してユーザーコントロールを作成しました。

    public static readonly DependencyProperty
    MyListProperty = DependencyProperty.Register(
        "MyList",
        typeof(ObservableCollection<Test>),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(new ObservableCollection<Test>())
        );

    public ObservableCollection<Test> MyList
    {
        get { return (ObservableCollection<Test>)base.GetValue(MyListProperty); }
        set { base.SetValue(MyListProperty, value); }
    }

    public static readonly DependencyProperty
    BProperty = DependencyProperty.Register(
        "B",
        typeof(string),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(null)
    );

    public string B
    {
        get { return (string)base.GetValue(BProperty); }
        set { base.SetValue(BProperty, value); }
    }

テストクラスは次のとおりです。

public class Test : DependencyObject
{
    public static readonly DependencyProperty
    AProperty = DependencyProperty.Register(
        "A",
        typeof(string),
        typeof(Test),
        new FrameworkPropertyMetadata(null)
    );

    public string A 
    {
        get { return (string)base.GetValue(AProperty); }
        set { base.SetValue(AProperty, value); }
    }
}

次に、バインドに自分のコントロールを使用しようとしています。

    <TextBox x:Name="tb1" Text="def"/>
    <my:UserControl1 x:Name="uc1" B="{Binding ElementName=tb1, Path=Text}">
        <my:UserControl1.MyList>
            <my:Test A="{Binding ElementName=tb1, Path=Text}"></my:Test>
            <my:Test A="100"></my:Test>
        </my:UserControl1.MyList>
    </my:UserControl1>

最初のバインディング(ユーザーコントロールのBプロパティを使用)は正しく機能します。問題は2番目のバインディングにあります(MyList要素であるTestのプロパティを使用)。デバッグするとき、MyListに2つの項目がありますが、最初の項目のAプロパティはnullです。ここで欠けているものを教えてください。

4

1 に答える 1

1

ここでの問題は、ElementName = tb1へのバインディングは、評価されない場合でも解決できないことです。ElementNameへのバインドは、WPFアプリケーションの視覚的または論理的なツリーにあるDependencyObjectsに対して解決されます。ObservableCollection(MyList)にアイテムを追加するということは、コレクションにアイテムを追加することだけを意味し、ビジュアルツリーには追加しないことを意味します。

編集:コメントで説明されているアプローチは次のとおりです。

ウィンドウ/ページ:

<Window.Resources>
    <!-- Declare the ViewModel as Resource -->
    <my:ViewModel x:Key="viewModel">
        <my:ViewModel.MyList>
            <my:Test A="Hello sweet" />
            <my:Test A="ViewModel" />
        </my:ViewModel.MyList>
    </my:ViewModel>
</Window.Resources>

<!-- Assign Ressource as DataContext -->
<StackPanel DataContext="{StaticResource viewModel}">

    <TextBox x:Name="tb1" Text="def"/>

    <!-- Reference your list within the ViewModel -->
    <ListBox ItemsSource="{Binding Path=MyList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- Bind your property  -->
                <TextBlock Text="{Binding Path=A}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

そして、ViewModelの実装:

public class ViewModel
{
    public ViewModel()
    {
        this.MyList = new ObservableCollection<Test>();
    }

    public ObservableCollection<Test> MyList { get; set; }
}

もちろん、クラスTestはDependencyObjectを実装する必要がなくなりました。単純なget/setプロパティは問題ありません。

于 2012-09-21T10:37:28.683 に答える