绑定源更改时 WPF 更新 UI
我有一个带有列表框和内容控件的 WPF 应用程序。内容控件内容绑定到列表框,并具有显示文本框的数据模板,该文本框的内容绑定到所述列表框中所选项目的变量。到目前为止,一切正常,即当我从列表框中选择一个项目时,文本框内容将更改为变量的当前值。但是,如果在运行时更改变量的值,则文本框不会更新,除非我选择另一个列表框项目,然后再次选择原始项目。关于我做错了什么或我在这里缺少什么有什么想法吗?我以为文本框的值会自动改变?非常感谢您的帮助。
下面是示例 (MainWindow.xaml)
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="12,105,0,0" x:Name="listBox1" VerticalAlignment="Top" Width="120" />
<ContentControl Height="120" HorizontalAlignment="Left" Margin="191,105,0,0" Name="contentControl1" VerticalAlignment="Top" Width="300" ContentTemplate="{DynamicResource MyDataTemplate}" Content="{Binding SelectedItem,ElementName=listBox1}"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="202,56,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
C# 代码:
public MainWindow()
{
InitializeComponent();
listBox1.Items.Add(new MyItem(32));
listBox1.Items.Add(new MyItem(45));
listBox1.Items.Add(new MyItem(5));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
((MyItem)listBox1.SelectedItem).Value = 4564654;
}
额外的类:
public class MyItem
{
public MyItem(Int32 Value)
{
this.Value = Value;
}
public Int32 Value { get; set; }
}
和模板:
我确信我错过了诸如通知 UI 源中的更改或以某种方式调用刷新之类的内容。这是我的实际问题的一个更加简化的版本,其中包括控件和标签等,当源更改时必须刷新这些控件和标签等。干杯:)
I have a WPF app with a listbox and a contentcontrol. The contentcontrol content is bound to the listbox and has a datatemplate that displays a textbox whose content is bound to a variable of the selected item in said listbox. So far, everything works well, i.e. when I select an item from the listbox the textbox content changes to the current value of the variable. However, if at runtime I change the value of the variable the textbox is not updated unless I select another listbox item and then select the original item again. Any ideas on what I am doing wrong or what I am missing here? I thought the value of the textbox would change automatically? Your help is much appreciated.
Here's the example (MainWindow.xaml)
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="12,105,0,0" x:Name="listBox1" VerticalAlignment="Top" Width="120" />
<ContentControl Height="120" HorizontalAlignment="Left" Margin="191,105,0,0" Name="contentControl1" VerticalAlignment="Top" Width="300" ContentTemplate="{DynamicResource MyDataTemplate}" Content="{Binding SelectedItem,ElementName=listBox1}"/>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="202,56,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
The C# code:
public MainWindow()
{
InitializeComponent();
listBox1.Items.Add(new MyItem(32));
listBox1.Items.Add(new MyItem(45));
listBox1.Items.Add(new MyItem(5));
}
private void button1_Click(object sender, RoutedEventArgs e)
{
((MyItem)listBox1.SelectedItem).Value = 4564654;
}
An extra class:
public class MyItem
{
public MyItem(Int32 Value)
{
this.Value = Value;
}
public Int32 Value { get; set; }
}
And the template:
Im sure I am missing something like notifying the UI of changes in the source or calling a refresh somehow. This is a much more simplified version of my real problem which includes controls and labels etc that must be refreshed when the source changes. Cheers :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 MyItem 类可能需要实现 INotifyPropertyChanged 接口。当 Value 属性发生变化时,调用 OnPropertyChanged("Value") 通知接口该值已发生变化,需要重新绘制。
You MyItem class probably needs to implement the
INotifyPropertyChanged
interface. When the Value property gets changed, call OnPropertyChanged("Value") to notify the interface that the value has changed and it needs to be redrawn.