绑定ListView以从另一个ListView过滤选定的项目

时间:2018-03-19 18:12:17

标签: c# wpf xaml listview textbox

我在WPF应用程序中有两个ListView项目,我正在开发。它们都连接到我的应用程序中Datasets ViewModel中的Presenter

我最初的想法是将我的SelectedItem加载到ListView TextBox中,然后将第二个ListView

过滤掉

但是,我遇到了一个问题:我希望按号码进行过滤,而TextBox似乎只想接收文字。有解决方法吗?

这是我的XAML

<Grid Margin="5,2,5,2" Height="350">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <ListView x:Name="TypeView" Grid.Column="0" ItemsSource="{Binding CashActivityTypes}" Margin="0,0,10,0">
        <ListView.View>
            <GridView>
                <!--This is the column I want to get -->
                <GridViewColumn Header="Sequence" DisplayMemberBinding="{Binding Sequence}" Width="75"/>
                <GridViewColumn Header="Activity Type" DisplayMemberBinding="{Binding Type}" Width="200"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" Width="Auto"/>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView Grid.Column="1" Margin="10,2,0,2">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Sequence" Width="75"/>
                <GridViewColumn Header="Activity" Width="200"/>
                <GridViewColumn Header="Description" Width="Auto"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
<Grid Margin="5,2,5,2">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding SelectedItem.Sequence, ElementName=TypeView, UpdateSourceTrigger=PropertyChanged}" Width="150" HorizontalAlignment="Left"/>
    <Button Grid.Column="1" Content="Adjust Daily Cash Activity" MinWidth="150" Height="20" HorizontalAlignment="Right"/>
</Grid>

1 个答案:

答案 0 :(得分:1)

你可以对第一个listview selectedItem属性进行双向绑定到VievModel:

private <TYPE> _CashActivityTypeSelected;

public <TYPE> CashActivityTypeSelected 
{
get
 {
   return _CashActivityTypeSelected;
 }
set
{
  _CashActivityTypeSelected=value; 
  FilterMySecondCollectionView(value);
 };
xaml中的

<ListView SelectedItem="{Binding CashActivityTypeSelected, mode=TwoWay}" x:Name="TypeView" Grid.Column="0" ItemsSource="{Binding CashActivityTypes}" Margin="0,0,10,0">
        <ListView.View>
            <GridView>
                <!--This is the column I want to get -->
                <GridViewColumn Header="Sequence" DisplayMemberBinding="{Binding Sequence}" Width="75"/>
                <GridViewColumn Header="Activity Type" DisplayMemberBinding="{Binding Type}" Width="200"/>
                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" Width="Auto"/>
            </GridView>
        </ListView.View>
    </ListView>

然后在FilterMySecondCollectionView(object t)中,您可以根据所选对象应用所需的任何过滤器。