OneWayToSource Binding seems broken in .NET 4.0

前端 未结 6 662
我寻月下人不归
我寻月下人不归 2020-11-27 05:45

OneWayToSource Binding seems broken in .NET 4.0

I have this simple piece of Xaml


    

        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 06:17

    Karl Shifflett's blog and @Simpzon's answer already cover why they added this feature and why it is not a problem for properties that always get what was set. In your own code you always use an intermediate property that has the proper semantics for binding and use an internal property that has the semantics you want. I would call the intermediate property a "blocking" property because it blocks the getter from reaching your internal property.

    But in the case where you don't have access to the source code for the entity that you are setting the property on and you want the old behavior, you can use a converter.

    Here is a blocking converter with state:

    public class BlockingConverter : IValueConverter
    {
        public object lastValue;
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return lastValue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            lastValue = value;
            return value;
        }
    }
    

    and you can use it for your example like this:

    
        
            
        
        
            
            

    Note that because the converter has a state you need a separate instance each time the resource is used and for this we can use the x:Shared="False" attribute on the resource.

提交回复
热议问题