将 ListView 标头数据模板中的文本框绑定到过滤器属性
我正在创建一个自定义的列表视图标题,其中包含标题文本,但也有一个文本框,您可以输入该文本框来过滤该列的内容。我的代码当前如下所示:
<UserControl.Resources>
<DataTemplate x:Key="myHeaderTemplate">
<StackPanel>
<TextBlock FontSize="14" Foreground="DarkBlue" Margin="20,4" Text="{Binding}" />
<TextBox Text="" Margin="4,2" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
这是包含文本框的标头数据模板的定义;和列表视图
<ListView ItemsSource="{Binding Path=MyData}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Last Name" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Something}" />
<GridViewColumn Header="First Name" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Something}" />
<GridViewColumn Header="Address" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Tube}" />
</GridView>
</ListView.View>
</ListView>
我希望能够构建一个可以应用于列表视图行的过滤器语句,但要做到这一点,我必须从标题模板中的每个过滤器文本框中获取数据。
我可以以某种方式将标题中的文本框绑定到视图模型上的属性吗?如果没有的话还有其他方法获取文本吗?
感谢您的任何帮助。
I am creating a customized listview header that has the header text but also has a textbox that you can enter to filter the content of that column. My code currently looks like this:
<UserControl.Resources>
<DataTemplate x:Key="myHeaderTemplate">
<StackPanel>
<TextBlock FontSize="14" Foreground="DarkBlue" Margin="20,4" Text="{Binding}" />
<TextBox Text="" Margin="4,2" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
which is the definition for the header datatemplate containing the texbox; and the listview
<ListView ItemsSource="{Binding Path=MyData}" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Last Name" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Something}" />
<GridViewColumn Header="First Name" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Something}" />
<GridViewColumn Header="Address" HeaderTemplate="{StaticResource myHeaderTemplate}"
DisplayMemberBinding="{Binding Path=Tube}" />
</GridView>
</ListView.View>
</ListView>
I want to be able to build up a filter statement that I can apply to the listview rows, but to do that I have to get the data from each filter textbox in the header template.
Can I somehow bind the textboxes in the headers to properties on my viewmodel? If not is there some other way to get the text?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够将标头绑定到如下属性:
需要
RelativeSource
才能访问ListView
的DataContext
- 您可以还给它一个名称并使用ElementName
代替。现在您可以创建一个
HeaderFilter
类:显然,当
Filter
更改以执行过滤时,您需要扩展该类以挂钩到事件。为对象上的每个列标题添加一个属性,该对象是
ListView
的DataContext
(可能是提供MyData
的同一对象)You should be able to bind the header to a property like this:
The
RelativeSource
is needed to get to theDataContext
of theListView
- you could also give it a name and useElementName
instead.Now you can make a
HeaderFilter
class:Obviously you would need to extend that class to hook into the event when
Filter
is changed to perform the filtering.Put a property for each column header on the object which is the
DataContext
for yourListView
(same object which providesMyData
probably)