OneWayToSource绑定似乎打破了.NET 4.0 [英] OneWayToSource Binding seems broken in .NET 4.0

查看:131
本文介绍了OneWayToSource绑定似乎打破了.NET 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OneWayToSource 绑定似乎打破了.NET 4.0

OneWayToSource Binding seems broken in .NET 4.0

我有这个简单的一块XAML代码

I have this simple piece of Xaml

<StackPanel>
    <TextBox Text="{Binding TextProperty, Mode=OneWayToSource}"/>
    <Button/>
</StackPanel>

和我的code背后看起来像这样

And my code behind looks like this

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
}
private string m_textProperty;
public string TextProperty
{
    get
    {
        return "Should not be used in OneWayToSource Binding";
    }
    set
    {
        m_textProperty = value;
    }
}

在.NET 3.5 这个工程,你可能除外。把一些文字在文本框,preSS Tab键,使其失去焦点,而 TextProperty 与任何更新那是在文本框输入的文本

In .NET 3.5 this works as you might except. Put some text in the TextBox, press Tab to make it lose Focus, and the TextProperty updates with whatever text that was entered in the TextBox

在.NET 4.0中,如果我输入一些文字文本框然后preSS Tab键,使其失去焦点时, 文本框恢复到值 TextProperty (即不应该OneWayToSource使用绑定​​)。这是重新阅读用于一个 OneWayToSource 绑定在.NET 4.0中?我只是想文本框它的价值推入 TextProperty ,而不是相反。

In .NET 4.0, if I type some text in the TextBox and then press Tab to make it lose Focus, the TextBox reverts to the value for TextProperty (meaning "Should not be used in OneWayToSource Binding"). Is this re-reading intended for a OneWayToSource Binding in .NET 4.0? I just want the TextBox to push its value into the TextProperty and not the other way around.

更新结果
加入悬赏这个问题,因为这已经成为我的项目一个市长的不便,我想知道,这种情况已经改变的原因。看来,绑定更新了源之后, GET 被调用。这是对理想行为的 OneWayToSource 绑定在.NET 4.0中?

Update
Adding a Bounty to this question as this has become a mayor inconvenience in my project and I would like to know the reason that this has changed. It seems that get is called after the Binding has updated the source. Is this the desired behavior for a OneWayToSource Binding in .NET 4.0?

如果是


  • 是什么与它在3.5的工作方式的问题?

  • 在什么情况下是这一新行为更好?

是这样,其实我们可以期望一个bug得到固定在未来的版本?

Or is this in fact a bug that we can hope to get fixed in a future release?

推荐答案

卡尔Shifflett的博客和@ Simpzon的回答已经涵盖为什么他们加入这个功能,为什么它是不适合的属性总是得到什么设置有问题。在您自己的code你总是使用对绑定正确的语义中间属性,并使用具有所需语义的内部属性。我称之为中间属性堵的性质,因为它会阻止吸气到达您的内部属性。

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.

但在情况下,你没有访问源$ C ​​$ C您所设置的财产实体和你想要的旧的行为,你可以使用一个转换器。

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.

下面是状态的拦截器:

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:

<Grid>
    <Grid.Resources>
        <local:BlockingConverter x:Key="blockingConverter" x:Shared="False"/>
    </Grid.Resources>
    <StackPanel>
        <TextBox Text="{Binding TextProperty, Mode=OneWayToSource, Converter={StaticResource blockingConverter}}"/>
        <Button Content="Click"/>
    </StackPanel>
</Grid>

请注意,由于转换器有一个状态,你需要一个单独的实例资源使用,为此我们可以使用 X每次:共享=FALSE属性上的资源。

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.

这篇关于OneWayToSource绑定似乎打破了.NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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