728x90
반응형

ListView 에 RadioButton 을 넣게 되면 아래 처럼 불필요하게 선택 항목에 배경이 생기게된다.

xaml 코드

<Window x:Class="Wpf.ListViewSelectItemTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Datas}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <RadioButton 
                        Content="{Binding}" 
                        IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                        GroupName="1"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

cs코드

using System.Collections.Generic;
using System.Windows;

namespace Wpf.ListViewSelectItemTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<string> Datas { get; set; }


        public MainWindow()
        {
            InitializeComponent();

            Datas = new List<string>();
            Datas.Add("1");
            Datas.Add("2");
            Datas.Add("3");
            Datas.Add("4");

            this.DataContext = this;
        }
    }
}

 

이를 아래 처럼 xaml 단에 처리를 하게되면 아래 처럼 선택항목의 배경을 없앨수 있다.

xaml 코드

<Window x:Class="Wpf.ListViewSelectItemTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Datas}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                                    <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <RadioButton 
                        Content="{Binding}" 
                        IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                        GroupName="1"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

소스
https://github.com/kei-soft/KJunBlog/tree/master/Wpf.ListViewSelectItemTest

728x90
반응형
Posted by kjun
TAG

댓글을 달아 주세요