ComboBox.SourceUpdated事件未触发 [英] ComboBox.SourceUpdated event is not fired

查看:215
本文介绍了ComboBox.SourceUpdated事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图中有两个组合框。它们都在ViewModel中绑定到两个不同的 ObservableCollections ,而当ComboBox1中的选定项目更改时,ComboBox2将使用不同的集合进行更新。绑定工作很好,但是,我想要第二个ComboBox总是选择其集合中的第一个项目。最初,它起作用,但是当ComboBox2中的源和项目被更新时,选择索引变为-1(即不再选择第一个项目)。



为了解决这个问题,我向ComboBox2添加了一个 SourceUpdated 事件,事件调用的方法将索引更改为0.问题是该方法从未被调用在方法的最上方的断点,它不会被击中)。这是我的XAML代码:

 < Grid> 
< StackPanel DataContext ={StaticResource mainModel}Orientation =Vertical>
< ComboBox ItemsSource ={Binding Path = FieldList}DisplayMemberPath =FieldName
IsSynchronizedWithCurrentItem =True/>

< ComboBox Name =cmbSelectorMargin =0,10,0,0
ItemsSource ={Binding Path = CurrentSelectorList,NotifyOnSourceUpdated = True}
SourceUpdated =cmbSelector_SourceUpdated>
< / ComboBox>
< / StackPanel>
< / Grid>

在代码隐藏中:

  //这个永远不会被调用
private void cmbSelector_SourceUpdated(object sender,DataTransferEventArgs e)
{
if(cmbSelector.HasItems)
{
cmbSelector.SelectedIndex = 0;
}
}

任何帮助都不胜感激。

解决方案

在工作了一个小时后,我终于明白了。答案基于以下问题:聆听依赖关系属性的更改



因此,您可以为对象上的任何 DependencyProperty 定义Property Changed事件。当您需要在控件中扩展或添加其他事件而不必创建新类型时,这可能非常有用。基本的过程是这样的:

  DependencyPropertyDescriptor descriptor = 
DependencyPropertyDescriptor.FromProperty(ComboBox.ItemsSourceProperty,typeof(ComboBox) );

descriptor.AddValueChanged(myComboBox,(sender,e)=>
{
myComboBox.SelectedIndex = 0;
});

这样做是创建一个 DependencyPropertyDescriptor 对象为 ComboBox.ItemsSource 属性,然后您可以使用该描述符为该类型的任何控件注册事件。在这种情况下,每次 ItemsSource 属性 myComboBox 更改时, SelectedIndex 属性设置为0(这表示列表中的第一个项目被选中。)


I have two ComboBoxes on my view. Both of them are bound to two different ObservableCollections in the ViewModel, and when the selected item in ComboBox1 is changed, ComboBox2 gets updated with a different collection. The binding works just fine, however, I want the second ComboBox to always select the first item in its collection. Initially, it works, however, when the source and items in ComboBox2 get updated, the selection index gets changed to -1 (i.e. the first item is no longer selected).

To fix this, I added a SourceUpdated event to ComboBox2 and method that the event calls changes the index back to 0. The problem is that the method is never called (I put a breakpoint at the very top of the method and it doesn't get hit). Here's my XAML code:

<Grid>
    <StackPanel DataContext="{StaticResource mainModel}" Orientation="Vertical">
        <ComboBox ItemsSource="{Binding Path=FieldList}" DisplayMemberPath="FieldName"
                  IsSynchronizedWithCurrentItem="True"/>

        <ComboBox Name="cmbSelector" Margin="0,10,0,0"
                  ItemsSource="{Binding Path=CurrentSelectorList, NotifyOnSourceUpdated=True}"
                  SourceUpdated="cmbSelector_SourceUpdated">
        </ComboBox>    
    </StackPanel>
</Grid>

And in the code-behind:

// This never gets called
private void cmbSelector_SourceUpdated(object sender, DataTransferEventArgs e)
{
    if (cmbSelector.HasItems)
    {
        cmbSelector.SelectedIndex = 0;
    }
}

Any help is appreciated.

解决方案

After working on it for an hour I finally figured it out. The answer is based on this question: Listen to changes of dependency property.

So basically you can define a "Property Changed" event for any DependencyProperty on an object. This can be extremely helpful when you need to extend or add additional events to a control without having to create a new type. The basic procedure is like this:

DependencyPropertyDescriptor descriptor = 
   DependencyPropertyDescriptor.FromProperty(ComboBox.ItemsSourceProperty, typeof(ComboBox));

descriptor.AddValueChanged(myComboBox, (sender, e) => 
{ 
   myComboBox.SelectedIndex = 0;
});

What this does is that it creates a DependencyPropertyDescriptor object for the ComboBox.ItemsSource property, and then you can use that descriptor to register an event for any control of that type. In this case, every time the ItemsSource property of myComboBox is changed, the SelectedIndex property is set back to 0 (which means the first item in the list is selected.)

这篇关于ComboBox.SourceUpdated事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆