简体   繁体   中英

Datagrid and Listview with same itemssource

I have a WPF application with a DataGrid and ListView that share the same ObservableCollection ItemsSource. When the DataGrid's CanUserAddRows property is True it causes the ListView to display the extra item that the DataGrid uses to add new rows.

How can I get the extra row from the DataGrid to not show in the ListView?

I tried using a trigger on the ListView's DataTemplate and checking if the items Id was empty or 0

`<ListView.ItemTemplate>
                <DataTemplate>
                    <Label Margin="-2,0,0,0" Name="CategoryLabel" >
                        <TextBlock TextWrapping="Wrap" Text="{Binding categoryName}" Height="46"></TextBlock>
                    </Label>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding categoryId}" Value="0" > <!-- also tried Value="" -->
                            <Setter TargetName="CategoryLabel" Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListView.ItemTemplate>` 

I just posted an answer to a problem of changing the template using a data template selector

Change View with its ViewModel based on a ViewModel Property

Possibly just because I have recently looked at this but I wonder if it might be possible to use the same technique here.

Have one template for where the category has a value,then another blank template for values without a category. The important part is you do the test in code rather than XAML so easier to inspect.

You can solve your problem without any modification of your ViewModel or code behind. You can do well without explicitly defining CollectionView's of any kind. Just add to your view's XAML one more (or only) DataTrigger that triggers on the NewItemPlaceholder item of the default view of ListView ItemsSource's collection. Have this trigger to set the UIElement.Visibility attached property to "Hidden". Place it within ItemContainerStyle style triggers. Like this:

<ListView 
    ItemsSource="{Binding ...}" 
>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
        ...
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" 
                        Value="{x:Static CollectionView.NewItemPlaceholder}">
                    <Setter Property="UIElement.Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="..." Value="{Binding ...}" />
            ...
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Label Margin="..." Name="...">
                            <TextBlock TextWrapping="Wrap"
                                Text="{Binding ...}" />
                        </Label>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM