简体   繁体   English

Datagrid和Listview具有相同的itemssource

[英]Datagrid and Listview with same itemssource

I have a WPF application with a DataGrid and ListView that share the same ObservableCollection ItemsSource. 我有一个WPF应用程序与DataGrid和ListView共享相同的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. 当DataGrid的CanUserAddRows属性为True时,它会导致ListView显示DataGrid用于添加新行的额外项。

How can I get the extra row from the DataGrid to not show in the ListView? 如何从DataGrid中获取额外的行以便不在ListView中显示?

I tried using a trigger on the ListView's DataTemplate and checking if the items Id was empty or 0 我尝试在ListView的DataTemplate上使用触发器并检查项ID是空还是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 使用ViewModel基于ViewModel属性更改视图

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. 重要的是你在代码而不是XAML中进行测试,因此更容易检查。

You can solve your problem without any modification of your ViewModel or code behind. 您可以在不修改ViewModel或代码的情况下解决您的问题。 You can do well without explicitly defining CollectionView's of any kind. 您可以在不明确定义任何类型的CollectionView的情况下完成。 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. 只需在ViewView ItemsSource集合的默认视图的NewItemPlaceholder项上触发一个(或唯一)DataTrigger添加到视图的XAML中。 Have this trigger to set the UIElement.Visibility attached property to "Hidden". 使用此触发器将UIElement.Visibility附加属性设置为“隐藏”。 Place it within ItemContainerStyle style triggers. 将它放在ItemContainerStyle样式触发器中。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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