1

私はこれに対する解決策を見つけることができません.. DataTemplate に ComboBox がある ListBox があります。DataBinding が用意されています。これは一種のコレクション シナリオのコレクションです。すべての ComboBox に「Select One Item」を付加したいと考えています。それ、どうやったら出来るの?

編集:上記の質問にコード/xamlが必要な理由が本当にわかりません。しかし、とにかく以下:

<Resources>
<ResourceDictionary>
            <DataTemplate x:Key="CategoriesDataTemplate">
                <StackPanel Orientation="vertical">
                    <TextBlock Text="{Binding Path=CategoryName}"></TextBlock>
                    <ComboBox ItemsSource="{Binding Path=Products}" Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName">
                    </ComboBox>
                </StackPanel>
            </DataTemplate>
</ResourceDictionary>
</Resources>
.....
<Grid..>
                <ListBox ItemsSource="{Binding Categories}" ItemTemplate="{DynamicResource CategoriesDataTemplate}">
</Grid>

カテゴリーごとに、カテゴリー名とその商品のコンボボックスを下に表示します。ユーザーは、カテゴリごとに 1 つの製品を選択できます。そのようなコンボボックスごとに、最初の項目を「製品の選択」などにしたいと思います。注:各カテゴリの各製品コレクションにアイテムを事前に保留せずにそれを行う方法があるかどうかを確認しています(可能であればソースコレクションを台無しにしたくありません)。ある種のイベント ハンドラー アプローチですか?

4

1 に答える 1

0

さらに掘り下げた後、以下を組み合わせたソリューションを得ました:

hbarc の提案Shimmy の解決策kmatyaszek の答え。トリックは CompositeCollection を使用することです。これにより、ComboBox に静的アイテムと動的アイテムの両方を使用できます。

私の ComboBox は現在、次のようになっています (StackPanel リソースで動作させることができませんでした。wpf を初めて使用するので、StackPanel リソースのアプローチについてコメントしてください。):

                <StackPanel.Resources>
                    <CollectionViewSource Source="{Binding Path=Products}" x:Key="options"/>
                    <!--not used. doesn't seem to be working when used DummyOption is a property in ViewModel-->
                    <CollectionViewSource Source="{Binding DummyOption}" x:Key="dummyOption"/>
                </StackPanel.Resources>

                <ComboBox Background="Transparent" SelectedValuePath="ProductId" DisplayMemberPath="ProductName" SelectedIndex="0">
                    <ComboBox.ItemsSource>
                        <CompositeCollection>
                            <!--works-->
                            <models:Product ProductName="Select" ProductId="{x:Static sys:Guid.Empty}" .../>
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding dummyOption}" />-->
<!--notworking-->
<!--<ComboBoxItem Content="{Binding DummyOption}" />-->
                            <!--notworking-->
                            <!--<ComboBoxItem Content="{Binding Source={StaticResource ResourceKey=dummyOption}}" />-->
                            <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=Products}}" />
                        </CompositeCollection>
                    </ComboBox.ItemsSource>    
                </ComboBox>
于 2013-11-01T23:31:34.720 に答える