ComboBox SelectedValue不会从绑定更改为依赖项属性 [英] ComboBox SelectedValue not changing from binding to Dependency Property

查看:102
本文介绍了ComboBox SelectedValue不会从绑定更改为依赖项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件:

我不想详细说明,为了简单起见,我有3个依赖项属性:

I don't want to get into specifics so for simplicity's sake i have 3 Dependency Properties :

MyCustomControl(CS):

public class MyCustomControl : Control
{
    DP Value1
    DP InternalValue
    DP SelectedValue 


   OnValue1Changed()
   {
       InternalValue = CalculateBasedOn1();
   }

   static bool _isSetInternally;  
   OnInternalValueChanged()
   {
       if(Condition())
       {
           _isSetInternally = true;
           SelectedValue = e.NewValue;
       }
       else
       {
           Value1 = FixValue1();                        
       } 
   }

   OnSelectedValueChanged()
   {
       if(_isSetInternally)
       {
          _isSetInternally = false;
          return; 
       }

       Value1 = ExtractValue1FromInput(e.NewValue);
   }

   public List<string> Values
   {
       get{ return new List<string>() { "1","2",......,"200"};}
   }

 }

我的ControlTemplate(再次简化):

    <ControlTemplate>
        <ComboBox x:Name="cb" 
           ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, Path=Values}"
           SelectedItem={Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
    </ControlTemplate>

问题:
cb显示最后选择的值,甚至在按以下说明修复后。

The Problem : cb is showing the last value chosen , even after it was fixed as explained below .

流:

1)输入:

1.1)SelectedValue绑定到DataContext中的一个属性,它会接收一个值。

1.1) SelectedValue is bound to a property in my DataContext , it receives a value.

1.2)OnSelectedValueChanged()设置Value1。

1.2) OnSelectedValueChanged() sets Value1.

1.3)Value1通过绑定设置 cb SelectedItem。

1.3) Value1 Sets "cb" SelectedItem via binding.

1.4 )OnValue1Changed设置InternalValue。

1.4) OnValue1Changed sets InternalValue.

1.5)OnInternalValueChanged()标志_isSetInternally = true,并更新SelectedValue。

1.5) OnInternalValueChanged() flags _isSetInternally = true and Updates SelectedValue.

1.6)OnSelectedValueChanged()零_isSetInternally = false并停止流(返回)。

1.6) OnSelectedValueChanged() zeros _isSetInternally = false and stops flow (return).

2 )输出:

2.1)cb的SelectedItem已更改。

2.1) cb's SelectedItem is changed.

2.2)通过绑定设置Value1。

2.2) Value1 is set via Binding.

2.3)OnValue1Changed()设置InternalValue。

2.3) OnValue1Changed() sets InternalValue.

2.4)如果满足条件,则传播输出。

2.4) If Condition is met propagate Output.

2.4.1)转到(1.4)

2.4.1) go to (1.4)

2.4.2)问题,条件尚未达到,请再次使用有效值设置Value1。

2.5)转到(1.4)

2.5) go to (1.4)

2.4.2中的问题是ComboBox仍显示(2.1)中选择的非有效值

The Problem in 2.4.2 is that The ComboBox is still showing the non - valid value chosen in (2.1)

探听我可以看到SelectedItem是正确的并且已被更改,但是SelectedValue和SelectedIndex仍然是Fix之前选择的。

Observing with snoop i can see that the SelectedItem is correct and have been changed , but the SelectedValue and SelectedIndex are still the one's chosen before the Fix.

*此外,我还尝试过强制将Value1设置为强制回调,效果相同。

*Further more iv'e attempted to Coerce Value1 on a Coercion Callback , it had the same effect.

在这种情况下,为什么ComboBox不会通过绑定更新其值?

Any idea's why the ComboBox doesn't update it's Value via Binding in this scenario ?

推荐答案

好吧,根据本文,问题似乎是ComboBox
中的一些同步问题:

O'k so the problem as it seems was some synchronization issue in the ComboBox according to this article :

http://ikriv.com/dev/wpf/ConfusedComboBox/index.shtml

特别感谢 Uriel Jacobson

建议使用Dispatcher.BeginInvoke(...)

it is recommended to make the update on the ComboBox asynchronously using Dispatcher.BeginInvoke(...)

 OnInternalValueChanged()
 {
     if(Condition())
     {
         _isSetInternally = true;
         SelectedValue = e.NewValue;
     }
     else
     {
         Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
         {
             Value1 = FixValue1();
         }));                      
     } 
 }

编辑:19.7.2014

Edit : 19.7.2014

尽管这样做确实可以解决此问题,但后来我以一种更为简单的方式重新编写了整个控件,
我做的其中一件事情就是删除了我放在该控件上的ValidationRule。绑定到组合框的SelectedItem。

Though this did patch and fix this issue , i later re-wrote the entire control in a much simpler fashion , one of the things i did was remove the ValidationRule i placed on the Binding to the Combobox's SelectedItem.

这样做时,我的DependencyProperty和Combox的SelectedItem之间的绑定会正常更新。
仅供参考,根据我的经验,我一起删除了WPF验证系统的使用,因为它们只是效果不好
(我当然通常使用其他词语来描述WPF验证,但这里不行:))。

When doing so , the Binding between my DependencyProperty and the Combox's SelectedItem updated normally. FYI , I removed the use of WPF Validation system all together in my experience they just don't work well (I of course usually use other words to describe WPF Validations , but not here :) ).

这篇关于ComboBox SelectedValue不会从绑定更改为依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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