gpt4 book ai didi

c# - 绑定(bind)到 Properties.Settings.Default.myString 的文本框文本不会更新 myString

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:00 24 4
gpt4 key购买 nike

我有一个字符串 Properties.Settings.Default.myString 和两个 TextBoxes

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

当我在 textBox1 中键入文本并更改焦点时,textBox2 中的文本将更新为我刚刚在 textBox1 中输入的文本。

让我感到困惑的是 Properties.Settings.Default.myString 从不更新我在任一文本框中输入的文本。更改后,我通过在调试器中检查 myString 确认了这一点。

我的问题是,为什么文本框中的这种变化没有反射(reflect)在绑定(bind)变量 myString 中?

完整的 XAML(WPF 应用程序):

<Window
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"
xmlns:local="clr-namespace:textBoxDataBinding"
xmlns:Properties="clr-namespace:textBoxDataBinding.Properties" x:Class="textBoxDataBinding.MainWindow"

mc:Ignorable="d"
Title="MainWindow" Height="212" Width="318">
<Window.DataContext>
<Properties:Settings/>
</Window.DataContext>
<Grid>

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

<!-- Button does: textBlock.Text = Properties.Settings.Default.myString; -->
<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="10,157,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="90,161,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
</Grid>

最佳答案

编辑

您没有绑定(bind)到 Default 单例设置,您正在创建一组新的设置。您需要绑定(bind)的是 Settings.Default 单例实例。

例如

 <Window DataContext="{x:Static properties:Settings.Default}">

替代方案:

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

由于历史原因的第一个答案:


您需要绑定(bind)到默认属性你需要确保要么

  1. DataContext 是您的 Properties.Settings.Default 对象。
  2. 将绑定(bind)源设为 Properties.Settings.Default

这对我有用:

<StackPanel>
<TextBox Text="{Binding Path=myStrring,Source={x:Static properties:Settings.Default}}"></TextBox>
<TextBox Text="{Binding Path=myStrring,Source={x:Static properties:Settings.Default}}"></TextBox>
</StackPanel>

或更简洁的语法:

<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}" />

关于c# - 绑定(bind)到 Properties.Settings.Default.myString 的文本框文本不会更新 myString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29048483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com