将 ListView 标头数据模板中的文本框绑定到过滤器属性

发布于 2024-10-31 19:33:57 字数 1420 浏览 1 评论 0原文

我正在创建一个自定义的列表视图标题,其中包含标题文本,但也有一个文本框,您可以输入该文本框来过滤该列的内容。我的代码当前如下所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-11-07 19:33:57

您应该能够将标头绑定到如下属性:

<GridViewColumn 
    Header="{Binding LastNameFilter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" 
    HeaderTemplate="{StaticResource myHeaderTemplate}"
    DisplayMemberBinding="{Binding Path=Something}" />

需要 RelativeSource 才能访问 ListViewDataContext - 您可以还给它一个名称并使用 ElementName 代替。

现在您可以创建一个 HeaderFilter 类:

public class HeaderFilter
{
    public string Name { get; set; }
    public string Filter { get; set; }
}

显然,当 Filter 更改以执行过滤时,您需要扩展该类以挂钩到事件。

为对象上的每个列标题添加一个属性,该对象是 ListViewDataContext(可能是提供 MyData 的同一对象)

public class SomeClass
{
    ....
    public HeaderFilter LastNameFilter { get; set; }
    ....
}

You should be able to bind the header to a property like this:

<GridViewColumn 
    Header="{Binding LastNameFilter, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" 
    HeaderTemplate="{StaticResource myHeaderTemplate}"
    DisplayMemberBinding="{Binding Path=Something}" />

The RelativeSource is needed to get to the DataContext of the ListView - you could also give it a name and use ElementName instead.

Now you can make a HeaderFilter class:

public class HeaderFilter
{
    public string Name { get; set; }
    public string Filter { get; set; }
}

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 your ListView (same object which provides MyData probably)

public class SomeClass
{
    ....
    public HeaderFilter LastNameFilter { get; set; }
    ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文