私は自分のプロパティを次のようにComboBoxDataContext
のにバインドしようとしています。SelectedItem
<ComboBox x:Name="ElementSelector"
ItemsSource="{Binding Source={StaticResource Elements}}"
DisplayMemberPath="ElementName"
SelectedItem="{Binding ValueElement, Mode=TwoWay}">
ここで、ElementsリソースはCollectionViewSource
(これが重要かどうかはわかりません)です。
すべてが初期化されると、のプロパティValueElement
はDataContext
の最初の項目に設定されますCollectionViewSource
。逆に初期化する必要がSelectedItem
あります。ComboBoxをプロパティの値に設定するか、一致するアイテムが含まれていない場合はnullに設定します。
これはどのように行うことができますか?
編集-追加情報:
ComboBoxはDataTemplateの一部です。
<DataTemplate x:Key="ReferenceTemplate"
DataType="viewModels:ElementMetaReferenceViewModel">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<ResourceDictionary>
<views:ElementsForReferenceViewSource x:Key="Elements"
Source="{Binding DataContext.CurrentProject.Elements, ElementName=Root}"
ReferenceToFilterFor="{Binding}"/>
</ResourceDictionary>
</StackPanel.Resources>
<TextBlock Text="{Binding PropertyName}"/>
<ComboBox x:Name="ElementSelector"
ItemsSource="{Binding Source={StaticResource Elements}}"
DisplayMemberPath="ElementName"
SelectedItem=""{Binding ValueElement, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
単純に、フィルタリングに使用される追加のDependencyPropertyElementsForReferenceViewSource
から派生しCollectionViewSource
、実装します。
のDataContext
アイテムは次のCollectionViewSource
ようになります。
public class ElementMetaReferenceViewModel : ViewModelBase<ElementMetaReference, ElementMetaReferenceContext>
{
...
private ElementMetaViewModel _valueElement;
public ElementMetaViewModel ValueElement
{
get { return _valueElement; }
set
{
if (value == null) return;
_valueElement = value;
Model.TargetElement = value.Model;
}
}
...
}