C# 当我以编程方式更改用户控件时,如何使数据绑定双向工作?

C# 当我以编程方式更改用户控件时,如何使数据绑定双向工作?,c#,wpf,xaml,data-binding,user-controls,C#,Wpf,Xaml,Data Binding,User Controls,我有一个WPF应用程序,它使用用户设置,并将其绑定到一个 <Window x:Class="SampleApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.

我有一个WPF应用程序,它使用用户设置,并将其绑定到一个

<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    <Grid>
        <TextBox x:Name="textbox" Text="{Binding Source={StaticResource Settings}, Path=Default.Folder}"/>
    </Grid>
</Window>
<Application x:Class="SampleApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:properties="clr-namespace:SampleApp.Properties"
             ShutdownMode="OnExplicitShutdown"
             Startup="Application_Startup">
    <Application.Resources>
        <properties:Settings x:Key="Settings"/>
    </Application.Resources>
</Application>
有一种方法非常有效:当窗口显示时,我的文本框总是显示MySetting的内容

另一种方法并不像我想的那样有效。当用户手动写入文本框时,它起作用

不起作用的是,当我以编程方式更改文本框时,如下所示:

<Window x:Class="SampleApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    <Grid>
        <TextBox x:Name="textbox" Text="{Binding Source={StaticResource Settings}, Path=Default.Folder}"/>
    </Grid>
</Window>
<Application x:Class="SampleApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:properties="clr-namespace:SampleApp.Properties"
             ShutdownMode="OnExplicitShutdown"
             Startup="Application_Startup">
    <Application.Resources>
        <properties:Settings x:Key="Settings"/>
    </Application.Resources>
</Application>
textbox.Text=folderBrowserDialog.SelectedPath; 在这种情况下,直到用户在文本框中键入内容,MySetting才会更新

我目前的解决方案是这样做:

Properties.Settings.Default.MySetting=textbox.Text; 但它破坏了双向数据绑定的意义

即使在以编程方式更改用户控件时,如何使数据绑定双向工作

但它破坏了双向数据绑定的意义

不,不是真的

双向绑定的要点是,当代码修改源属性时,UI的目标属性将更新;当用户修改目标属性时,源属性将更新

双向绑定肯定不存在,因此您可以通过编程方式将值分配给目标属性,并将其反映在源属性中,而源属性本来可以且应该进行设置

编写WPF代码的人很少需要命名XAML中定义的UI元素或与之交互。当这种情况发生时,应该只实现用户界面功能,如拖放、按键处理等

换句话说:在MVVM范例中,视图模型数据结构是非UI代码应该处理的唯一事情。绑定机制在业务逻辑(由视图模型表示)和用户界面(由XAML表示)之间提供了中介,业务逻辑由视图模型和视图模型后面的模型表示,用户界面由XAML表示

事实上,通常如果显式设置目标属性,它会放弃绑定,导致绑定根本无法工作


因此,正确的方法是,在代码隐藏中,只与MySetting属性交互。如果要更新显示给用户的值,则需要更改绑定到该值的代码隐藏属性。

是否确实使用了{StaticResource Settings}?更典型的做法是使用{x:Static local:Properties.Settings}。你真的在这里发布了真实的代码吗?也就是说,直接从文件中复制/粘贴?你能确保你包括一个真实的吗?@PeterDuniho。我是WPF的初学者。我只是跟着报纸上写的。对不起,我不知道还有别的方法可以做到这一点。在任何情况下,我已经更新了我的问题。因此总结一下:数据绑定将相应地更新文本框,而不是textbox.Text=folderBrowserDialog.SelectedPath do Properties.Settings.Default.MySetting=folderBrowserDialog.SelectedPath。明白了,谢谢。没错。对不起,上面我没有说得更清楚。