简体   繁体   English

在 wpf 中绑定多个源元素的依赖属性

[英]Binding multiple source an element's dependency properties in wpf

Is it possible to bind multiple source to an element's dependecy properties?是否可以将多个源绑定到元素的依赖属性?

In my case, I have a combobox control its ItemSource property filled by a viewmodel, however the Text property is binded to a model which is used by an ItemsControl.在我的例子中,我有一个 combobox 控件,它的 ItemSource 属性由一个视图模型填充,但是 Text 属性绑定到一个由 ItemsControl 使用的 model。

Thank you.谢谢你。

xaml snippet: xaml 片段:

<DataTrigger Binding="{Binding EnumType}" Value="6">
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>                
            <ComboBox    
                x:Name="ListOfItems"  
                IsDropDownOpen="{Binding IsDropDownOpen,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"
                StaysOpenOnEdit="True"
                IsTextSearchEnabled="False"
                IsReadOnly="False"
                IsEditable="True"
                ItemsSource="{Binding Path=FilteredSource,  Source={StaticResource ItemsVM} ,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                Text="{Binding   Path=Result, UpdateSourceTrigger=PropertyChanged}"                 
                DisplayMemberPath="{Binding  Path=Item.Name,  Source={StaticResource ItemsVM}}" >                 

                <ComboBox.Triggers>
                    <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames   Storyboard.TargetProperty="IsDropDownOpen">
                                    <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ComboBox.Triggers>
            </ComboBox>
        </DataTemplate>
    </Setter.Value>
</Setter>

You can set the Source of each binding like you are currently doing for the ItemsSource property:您可以像当前对ItemsSource属性所做的那样设置每个绑定的Source

ItemsSource="{Binding Path=FilteredSource, Source={StaticResource ItemsVM}}"

If you don't do this, the framework will look for the FilteredSource property in the current DataContext of the ComboBox .如果您不这样做,框架将在ComboBox的当前DataContext中查找FilteredSource属性。

By the way, the DisplayMemberPath property should be set to a string that specifies the name of a property of an item in the ItemsSource :顺便说一句, DisplayMemberPath属性应该设置为一个string ,该字符串指定ItemsSource中项目的属性名称:

DisplayMemberPath="Name"

You should not bind to this property.您不应绑定到此属性。

It also makes no sense to set the UpdateSourceTrigger property for an ItemsSource binding to PropertyChanged , or set the Mode to TwoWay , since the control won't set the source property.ItemsSource绑定的UpdateSourceTrigger属性设置为PropertyChanged或将Mode设置为TwoWay也是没有意义的,因为控件不会设置源属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM