如何更新我的C#WPF绑定? [英] How to update my C# WPF binding?
本文介绍了如何更新我的C#WPF绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我是WPF的新手.
如您在这张图片中看到的:
http://api.ning.com/files/3FX8CRo2Q3IZ ...
我有一个过滤列表框项目的文本框.
这是我的WPF代码:
Hi
I am new in WPF.
As you see in this picture:
http://api.ning.com/files/3FX8CRo2Q3IZ...
I have a textbox that filters items of listbox.
This is my WPF code:
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="28,31,0,0" Name="textBlock1" Text="Filter Tags:" VerticalAlignment="Top" />
<TextBox Text="{Binding Path=FilterText,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="91,28,0,0" Name="textBoxFilterTags" VerticalAlignment="Top" Width="120" TextChanged="textBoxFilterTags_TextChanged" />
<ListBox ItemsSource="{Binding Path=Filter}" Height="178" HorizontalAlignment="Left" Margin="12,71,0,0" Name="listBoxTags" VerticalAlignment="Top" Width="254">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=Tick,Mode=TwoWay}" Margin="3,1,1,1" />
<TextBlock Text="{Binding Path=Name}" Margin="3,1,1,1" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
我的列表框绑定到一个名为:
的属性
my listbox is binded to a property called:
public ObservableCollection<tagtick> Filter
{
get
{
.... // filtering based on filter text
}
}
现在,我的问题是,当我输入过滤器文本时,列表框应更新过滤器.我尝试使用UpdateSourceTrigger,但没有成功.有人可以告诉我文本框文本更改时如何触发绑定更新吗?
预先感谢.
Now, my problem is that when I type in the filter text, the listbox should update the filter. I tried using UpdateSourceTrigger but it didnt work. can some one tell me how to trigger the binding update when the text of textbox change?
Thanks in advance.
推荐答案
建议通过代码控制此绑定,原因之一是您的ListBox的DataTemplate已更改为StackPanel的父元素. (内部有两个其他元素)和StackPanel中的TextBlock可能无法直接访问,特别是对于TwoWay绑定,如果不进行代码强制转换的话.我建议您通过代码而不是XAML进行过滤.
It''s advisable to control this binding through code, one reason being that the DataTemplate of your ListBox have changed to a parent element of StackPanel(with two other element inside) and the TextBlock inside the StackPanel may not be directly accessible especially for a TwoWay Binding without doing some casting in code. I suggest that you do your filtering through code and not XAML.
这篇关于如何更新我的C#WPF绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文