Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF删除选中项目的Listview边框_C#_Wpf_Xaml - Fatal编程技术网

C# WPF删除选中项目的Listview边框

C# WPF删除选中项目的Listview边框,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试将功能添加到所选的ListViewItems。如果选择了该项,则该项应流入其旁边的视图/窗格(删除列表视图右侧边框) 例如:此选定项的行为应为,但右侧边框不应存在 这就是它应该看起来的样子 这是我的代码: 视图: 视图模型: public partial class MainWindow : Window { private List<Employee> employees; public MainWindow() { Ini

我正在尝试将功能添加到所选的
ListViewItems
。如果选择了该项,则该项应流入其旁边的视图/窗格(删除
列表视图
右侧边框)

例如:此选定项的行为应为,但右侧边框不应存在

这就是它应该看起来的样子

这是我的代码:

视图:


视图模型:

public partial class MainWindow : Window
{
    private List<Employee> employees;

    public MainWindow()
    {
        InitializeComponent();
        employees = new List<Employee>();
        employees.Add(new Employee { Id = "Header One", Name = "First" });
        employees.Add(new Employee { Id = "Header Two", Name = "Second" });
        employees.Add(new Employee { Id = "Header Three", Name = "Third" });
        employees.Add(new Employee { Id = "Header Four", Name = "Fourth" });
        employees.Add(new Employee { Id = "Header Five", Name = "Fifth" });

        this.DataContext = this;
        myListView.ItemsSource = employees;

        CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);
        PropertyGroupDescription groupDescription = new PropertyGroupDescription("Id");
        view.GroupDescriptions.Add(groupDescription);
    }

    public List<Employee> Employees
    {
        get
        {
            return employees;
        }
    }
}
公共部分类主窗口:窗口
{
私人名单雇员;
公共主窗口()
{
初始化组件();
雇员=新名单();
添加(新员工{Id=“Header One”,Name=“First”});
添加(新员工{Id=“Header Two”,Name=“Second”});
添加(新员工{Id=“Header Three”,Name=“Third”});
添加(新员工{Id=“Header Four”,Name=“Fourth”});
添加(新员工{Id=“Header-Five”,Name=“Fifth”});
this.DataContext=this;
myListView.ItemsSource=员工;
CollectionView视图=(CollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);
PropertyGroupDescription groupDescription=新的PropertyGroupDescription(“Id”);
view.groupDescription.Add(groupDescription);
}
公开名单雇员
{
得到
{
返回员工;
}
}
}
样式:

<Application.Resources>

    <Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
        <Setter Property="Background" Value="LightBlue"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Padding" Value="0 5 0 0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="0" SnapsToDevicePixels="true">
                        <ScrollViewer Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bd" Value="Red"/>
                            <Setter Property="BorderBrush" TargetName="Bd" Value="Black"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsGrouping" Value="true"/>
                                <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="White"/>
                            <Setter Property="BorderBrush" Value="Black"/>
                            <Setter Property="BorderThickness" Value="0 1 0 1"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="Gray"/>
                            <Setter Property="Foreground" Value="Pink"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="Aqua"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" Value="Black"/>
                            <Setter Property="BorderThickness" Value="0 1 0 1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="GroupCustomStyle1" TargetType="{x:Type GroupItem}">
        <Setter Property="Margin" Value="0 8 0 0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <DockPanel Margin="0,0,0,2">
                            <Border BorderBrush="Black" BorderThickness="0, 0, 0, 1" Width="400" HorizontalAlignment="Center" Padding="5 3 3 5">
                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
                            </Border>
                        </DockPanel>
                        <ItemsPresenter />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Application.Resources>


我不知道实现这一点的最佳方法是什么,但我非常肯定它最终会以一次肮脏的黑客攻击而告终

列表视图中删除右边框
,并将其添加到
组项目中

<Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="LightBlue"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="BorderThickness" Value="1 1 0 1"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Padding" Value="0 5 0 0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="0" SnapsToDevicePixels="true">
                    <DockPanel>
                        <ScrollViewer DockPanel.Dock="Top" Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                        <Grid Width="1" SnapsToDevicePixels="True" DockPanel.Dock="Bottom" 
                                      HorizontalAlignment="Right" Background="{TemplateBinding BorderBrush}" />
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="Red"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="Black"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsGrouping" Value="true"/>
                            <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="White"/>
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="BorderThickness" Value="0 1 0 1"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="Gray"/>
                        <Setter Property="Foreground" Value="Pink"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="Aqua"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="BorderThickness" Value="0 1 0 1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<Style x:Key="GroupCustomStyle1" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <StackPanel>
                    <Border BorderThickness="0 0 1 0" BorderBrush="Black">
                        <DockPanel Margin="0,8,0,2">
                            <Border BorderBrush="Black" BorderThickness="0, 0, 0, 1" Width="400" HorizontalAlignment="Center" Padding="5 3 3 5">
                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
                            </Border>
                        </DockPanel>
                    </Border>
                    <ItemsPresenter />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


所以它只是所选项目右侧的边框?如果将所选元素的边框颜色设置为白色,该怎么办?请尝试。。但它不会与ListView边框重叠。不管我设置的边界有多大@弗雷德莫:是的,太完美了!真不敢相信我错过了。。回答得好,谢谢@嗯八
<Style x:Key="ListViewStyle1" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="LightBlue"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="BorderThickness" Value="1 1 0 1"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Padding" Value="0 5 0 0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListView}">
                <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="0" SnapsToDevicePixels="true">
                    <DockPanel>
                        <ScrollViewer DockPanel.Dock="Top" Focusable="false">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                        <Grid Width="1" SnapsToDevicePixels="True" DockPanel.Dock="Bottom" 
                                      HorizontalAlignment="Right" Background="{TemplateBinding BorderBrush}" />
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="Red"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="Black"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsGrouping" Value="true"/>
                            <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="White"/>
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="BorderThickness" Value="0 1 0 1"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="Gray"/>
                        <Setter Property="Foreground" Value="Pink"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="Aqua"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="BorderThickness" Value="0 1 0 1"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<Style x:Key="GroupCustomStyle1" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <StackPanel>
                    <Border BorderThickness="0 0 1 0" BorderBrush="Black">
                        <DockPanel Margin="0,8,0,2">
                            <Border BorderBrush="Black" BorderThickness="0, 0, 0, 1" Width="400" HorizontalAlignment="Center" Padding="5 3 3 5">
                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
                            </Border>
                        </DockPanel>
                    </Border>
                    <ItemsPresenter />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>