MainWindow.xaml ファイルがあります。
<Window.Resources>
<CollectionViewSource x:Key="cvs"
Source="{Binding Source={StaticResource ResourceKey=DetailsCollection}}" />
<CollectionViewSource x:Key="DetailScopes">
<CollectionViewSource.Source>
<ObjectDataProvider
MethodName="GetValues"
ObjectType="{x:Type system:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="entities:DetailScope" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</CollectionViewSource.Source>
</CollectionViewSource>
<DataTemplate x:Key="AccountDetail"
DataType="{x:Type entities:AccountDetail}">
<DockPanel>
<ComboBox
DockPanel.Dock="Left"
ItemsSource="{Binding Source={StaticResource ResourceKey=DetailScopes}}"
SelectedItem="{Binding Path=Scope}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Converter={StaticResource DetailScopeConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Text="{Binding Path=Value}" />
</DockPanel>
</DataTemplate>
</Window.Resources>
...
<ListBox
ItemTemplate="{StaticResource ResourceKey=AccountDetail}"
ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}" />
およびそのコード ビハインド クラスで、詳細スコープのフィルターを定義しました。
public class MainWindow
{
public MainWindow()
{
CollectionViewSource detailScopes;
InitializeComponent();
// Attach filter to the collection view source
detailScopes = this.Resources["DetailScopes"] as CollectionViewSource;
detailScopes.Filter += new FilterEventHandler(DetailScopesFilter);
private void DetailScopesFilter(object sender, FilterEventArgs e)
{
DetailScope scope;
scope = (DetailScope)e.Item;
if (scope == DetailScope.Private ||
scope == DetailScope.Business)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
}
}
次に、AccountDetail
クラスがあります。
public class AccountDetail
{
public string Value
{
get { return this.value; }
set { this.value = value; }
}
public DetailScope Scope
{
get { return scope; }
set { scope = value; }
}
private string value;
private DetailScope scope;
}
最後に、列挙型:
public enum DetailScope
{
Private,
Business,
Other
}
コードを実行すると、一連のアカウントの詳細が入力されたリスト ボックスが表示されます。それぞれのリスト ボックスには、選択されたスコープを持つ独自のコンボ ボックスと、適切な値を持つテキスト ボックスがあります。問題は、コンボ ボックスで選択されたすべての値が、最後に入力された詳細に設定された範囲と一致し、コンボ ボックスの値を変更すると、それらがすべて同じアカウントの詳細にバインドされているかのように、すべての値が更新されることです。
ObjectDataProvider
DetailScopesから取り出して、CollectionViewSource
AccountDetail のコンボ ボックスに直接バインドすると、問題はなくなりました。ただし、フィルタリングを適用していて、フィルタリングを に適用できないため、 内で必要です。ItemsSource
DataTemplate
CollectionViewSource
ObjectDataProvider
誰かがなぜこれが起こっているのか、実際にどのように接続すればよいのか説明してもらえますCollectionViewSource
かObjectDataProvider
? ありがとうございました。