【问题标题】:Textbox text binding to Properties.Settings.Default.myString does not update myString绑定到 Properties.Settings.Default.myString 的文本框文本不会更新 myString
【发布时间】:2015-05-16 21:27:51
【问题描述】:

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

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

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

让我感到困惑的是Properties.Settings.Default.myString 永远不会随着我在任一文本框中输入的文本而更新。我确实通过在更改后在调试器中检查myString来确认这一点。

我的问题是,为什么 textBox 中的这种变化没有反映在绑定变量 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>

【问题讨论】:

  • 我做到了,但这不是问题,因为Save() 只会保存myString 的当前状态。但是myString 永远不会更新。不过,感谢您的提示。

标签: c# wpf xaml data-binding


【解决方案1】:

编辑

您没有绑定到Default 单例设置,您正在创建一组新设置。你需要绑定的是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}"/>

历史原因的第一个答案:


您需要绑定到默认属性 您需要确保

  1. DataContext 是您的 Properties.Settings.Default 对象。
  2. 将绑定源设为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}" />

【讨论】:

  • 也许你误解了我的问题。数据绑定本身是有效的。我说过 textBox1 上的更改反映在 textBox2 中。但是源永远不会更新以反映这种变化。
  • 你的来源是什么?你能显示你在哪里设置 DataContext
  • 您的解决方案不适用于 WPF,但您让我走上了正轨!我编辑了你的答案并标记了它。我有一个问题。使用 Visual Studio 数据绑定创建对话框 (i.imgur.com/TgfVn2A.png) 时,为什么显示“错误”设置(标记为蓝色)?
  • 我的答案来自一个全新的 WPF 项目,该项目编译并运行:|可能有一些命名空间的变化......
猜你喜欢
  • 2011-09-22
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 2016-04-20
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多