C# 文本框文本绑定到Properties.Settings.Default.myString不会更新myString

C# 文本框文本绑定到Properties.Settings.Default.myString不会更新myString,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我得到了一个字符串Properties.Settings.Default.myString和两个文本框 <TextBox x:Name="textBox1" Text="{Binding myString}"/> <TextBox x:Name="textBox2" Text="{Binding myString}"/> 编辑 您没有绑定到默认的单例设置,而是创建了一组新的设置。您需要绑定到的是Settings.Default singleton实例 例如 或更简洁的语

我得到了一个字符串Properties.Settings.Default.myString和两个文本框

<TextBox x:Name="textBox1" Text="{Binding myString}"/>
<TextBox x:Name="textBox2" Text="{Binding myString}"/>
编辑

您没有绑定到默认的单例设置,而是创建了一组新的设置。您需要绑定到的是Settings.Default singleton实例

例如

或更简洁的语法:

<Window x:Class="WpfApplication1.SO29048483"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:properties="clr-namespace:WpfApplication1.Properties"
        Title="SO29048483" Height="300" Width="300">
    <StackPanel DataContext="{x:Static properties:Settings.Default}">
        <TextBox Text="{Binding myStrring}" />
        <TextBox Text="{Binding myStrring}" />
    </StackPanel>
</Window>
如果你想在失去焦点之前把它拍下来:

    <TextBox Text="{Binding myStrring, UpdateSourceTrigger=PropertyChanged}" />
编辑

您没有绑定到默认的单例设置,而是创建了一组新的设置。您需要绑定到的是Settings.Default singleton实例

例如

或更简洁的语法:

<Window x:Class="WpfApplication1.SO29048483"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:properties="clr-namespace:WpfApplication1.Properties"
        Title="SO29048483" Height="300" Width="300">
    <StackPanel DataContext="{x:Static properties:Settings.Default}">
        <TextBox Text="{Binding myStrring}" />
        <TextBox Text="{Binding myStrring}" />
    </StackPanel>
</Window>
如果你想在失去焦点之前把它拍下来:

    <TextBox Text="{Binding myStrring, UpdateSourceTrigger=PropertyChanged}" />

是的,但这不是问题所在,因为Save只会保存myString的当前状态。但myString从不更新。不过,谢谢你的提示。我做了,但这不是问题所在,因为Save只会保存myString的当前状态。但myString从不更新。不过,谢谢你的提示。也许你误解了我的问题。数据绑定本身正在工作。我说过textBox1上的更改反映在textBox2中。但是来源永远不会更新以反映这种变化。你的来源是什么?你能告诉我你在哪里设置DataContext吗?你的解决方案不适用于WPF,但你让我走上了正确的轨道!我编辑了你的答案并做了标记。不过我有一个问题。当使用Visual Studio数据绑定创建对话框时,为什么会显示错误的蓝色设置?我的答案来自一个全新的WPF项目,该项目已编译并运行:|可能有一些名称空间更改……可能您误解了我的问题。数据绑定本身正在工作。我说过textBox1上的更改反映在textBox2中。但是来源永远不会更新以反映这种变化。你的来源是什么?你能告诉我你在哪里设置DataContext吗?你的解决方案不适用于WPF,但你让我走上了正确的轨道!我编辑了你的答案并做了标记。不过我有一个问题。使用Visual Studio数据绑定创建对话框时,为什么会显示错误的蓝色设置?我的答案来自一个全新的WPF项目,该项目已编译并运行:|可能是一些名称空间更改。。。。
<Window x:Class="WpfApplication1.SO29048483"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:properties="clr-namespace:WpfApplication1.Properties"
        Title="SO29048483" Height="300" Width="300">
    <StackPanel DataContext="{x:Static properties:Settings.Default}">
        <TextBox Text="{Binding myStrring}" />
        <TextBox Text="{Binding myStrring}" />
    </StackPanel>
</Window>
    <TextBox Text="{Binding myStrring, UpdateSourceTrigger=PropertyChanged}" />