0

私はこのDataTemplateを持っています:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Model="clr-namespace:Effectus.Model"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
>

<DataTemplate DataType="{x:Type Model:ToDoAction}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Title" 
                   Grid.Row="0" Grid.Column="0"/>
        <TextBox Text="{Binding Path=Title}"
                   IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
                   Grid.Row="0" Grid.Column="1"/>

        <TextBlock Text="Content" 
                   Grid.Row="1" Grid.Column="0"/>
        <TextBox Text="{Binding Path=Content}"
            IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
            AcceptsReturn="True"
            MinHeight="100"
            Grid.Row="2" Grid.ColumnSpan="2"/>

        <TextBlock Text="Owner"
                   Grid.Row="6" Grid.Column="0"/>
        <ComboBox SelectedItem="{Binding Path=Owner}" DisplayMemberPath="Username" SelectedValuePath="Username"
                  ItemsSource="{Binding Converter={StaticResource EnumValuesConverter}, ConverterParameter={x:Type Model:Status}}"
                  IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},Path=DataContext.AllowEditing.Value}"
                  Grid.Row="6" Grid.Column="1"/>

    </Grid>
</DataTemplate>

最後の部分では、Text が「Owner」である TextBlock と、それに続く ComboBox を確認できます。コンテキストを提供するために、これは現在取り組んでいる ToDo アプリの一部です (MVVM に取り込もうとしています)。私が投稿した XAML は、ToDoAction オブジェクトの DataTemplate です。「所有者」の ComboBox にすべてのユーザーが入力できるようにします。NHibernate を介して DB からそれらを取得できますが、DataTemplate を DataSource (私の場合は Nhibernate セッションですが、これはより一般的だと思います) にバインドする方法について、最もかすかなアイデアはありません。少しアドバイスをお願いできますか?本当にありがとうございました!

4

1 に答える 1

1

私が使用したことのないObjectDataProviderのように、それを処理する方法は複数あります。しかし、シングルトンを使用するのが最も簡単で最速だと思います。

public sealed class UserList
{
    public ObservableCollection<User> Users {get; private set;}

    public static UserList Instance
    {
        get{return sInstance;}
    }

    private static UserList sInstance = new UserList();
}

ユーザー リストは通常​​どおり更新および変更できますが、このコレクションへのバインディングもそうである必要があります。

ItemsSource="{Binding Source={x:Static my:UserList.Instance}, Path=Users}
于 2012-11-28T12:07:17.230 に答える