c# - 使用 Selected DataTemplate 在 ListView 中选择对象

标签 c# wpf listview datatemplate selected

我在 ListView 上为 SelectedItem 使用 DataTemplate 作为:

<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
<WrapPanel (...) BackGround="Silver">

</WrapPanel>
(...)
<Style TargetType="ListViewItem">
       <Style.Triggers>
             <MultiTrigger>
                   <MultiTrigger.Conditions>
                       <Condition Property="IsSelected" Value="true" />
                       <Condition Property="Selector.IsSelectionActive" Value="true" />
                   </MultiTrigger.Conditions>
                   <Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
             </MultiTrigger>
       </Style.Triggers>
</Style>
</ControlTemplate>

此外,ItemsSource 绑定(bind)到 IObservableCollection。但是我在选择 myListView 中的项目时遇到了一些问题。我想要达到的是,默认项目模板有白色背景。选定的背景是银色。当我点击项目时,背景发生变化。但是,当我从代码中执行此操作时,listview 已经选择了项目(已选中,selected index = 0,selectetitem != null),但是项目从未选择的项目中获取样式。所以基本上我想用 selectedTemplate 选择项目。 我试过 myListView.SelectedIndex、myLisview.SelectedItem,但实际上没有用。有什么想法吗?

谢谢!

最佳答案

如果我对你的理解是正确的,那么当你选择列表中的项目时,会显示所选模板银色背景,但是当你使用代码设置所选项目时,它不会显示?

我创建了一个简单的示例,我必须做的更改是将 listview 上的 selecteditem 属性的绑定(bind)设置为两种方式..

<ListView SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">

我的示例使用 Caliburn Micro,但这并不重要。

这是我的示例代码,演示了它的工作...

查看模型:

public class ShellViewModel : Screen, IShell
{
    private ObservableCollection<Person> people = new ObservableCollection<Person>();

    public ObservableCollection<Person> People
    {
        get
        {
            return this.people;
        }

        set
        {
            this.people = value;
            this.NotifyOfPropertyChange(() => this.People);
        }
    }

    private Person selectedPerson;

    public Person SelectedPerson
    {
        get
        {
            return this.selectedPerson;
        }

        set
        {
            this.selectedPerson = value;
            this.NotifyOfPropertyChange(() => this.SelectedPerson);
        }
    }

    public ShellViewModel()
    {
        var russell = new Person { Name = "Russell" };
        this.People.Add(new Person { Name = "Benjamin" });
        this.People.Add(new Person { Name = "Steve" });
        this.People.Add(russell);
        this.SelectedPerson = russell;
    }

查看:

<Window x:Class="WpfApplication5.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Window.Resources>

    <Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <DataTrigger
                    Binding="{Binding
                        RelativeSource={RelativeSource
                            Mode=FindAncestor,
                            AncestorType={x:Type ListBoxItem}},
                            Path=IsSelected}"
                    Value="True">
                <Setter Property="Background" Value="Red"></Setter>
                </DataTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="MyItemTemplate">
        <TextBlock Text="{Binding Name}" Style="{StaticResource TextStyle}"></TextBlock>
    </DataTemplate>
</Window.Resources>

<Grid Background="White">
    <ListView ItemsSource="{Binding People}" x:Name="People" ItemTemplate="{StaticResource MyItemTemplate}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}">
    </ListView>
</Grid>

更改 View 模型上的 SelectedPerson 属性也会显示在 View 中。

希望这对您有所帮助!

关于c# - 使用 Selected DataTemplate 在 ListView 中选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111594/

相关文章:

c# - 没有虚拟化的 WPF DataGrid 性能

c# - 在 ListView 中搜索字符串和过滤器

WPF ListView ScrollViewer 双击事件

c# - 将 wpf View 保存为图像,最好是 .png

java - 如何获取鼠标指针下的(ListView 的)单元格?

c# - asp.net core 2.0 dotvvm vs razor pages - 对于交互式网络应用程序来说,哪个更强大?

C#,可空十进制

javascript - 服务器端的 AngularJS 变量

c# - MultiView 和 GridView 分页问题

c# - 删除 "/*"和 "*/"之间的文本( block 注释)