使用Selected DataTemplate在ListView中选择对象

use*_*657 5 c# wpf listview datatemplate selected

我在ListView上使用DataTemplate for SelectedItem:

<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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

Ben*_*aul 5

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

我创建了一个有效的简单示例,我必须做的更改是将listite上的selecteditem属性的绑定设置为双向...

<ListView SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
Run Code Online (Sandbox Code Playgroud)

我的例子使用了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;
    }
Run Code Online (Sandbox Code Playgroud)

视图:

<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>
Run Code Online (Sandbox Code Playgroud)

更改视图模型上的SelectedPerson属性也会显示在视图中.

希望这可以帮助!