Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Checkbox in cell row readonly possible?

Tags:

wpf

I have a ListView which is bind dynamically to a list of object of the same type.

The object have a boolean value.

There a ListView column which display a checkbox instead of the "true" and "false" normal value for that specific property.

Is there a way to set that checkbox readonly ? otherwise is there a way to tell that the click is coming from this specific row in the events "checked" and "unchecked" which execute a method in code behind ?

Thanks!

like image 322
Rushino Avatar asked Nov 28 '22 18:11

Rushino


1 Answers

You can make any control readonly by setting IsHitTestVisible and Focusable to false.

XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1">

    <StackPanel>
        <ListView ItemsSource="{Binding}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Is Valid">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsValid}" IsHitTestVisible="False" Focusable="False" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn> 
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>

</Window>

Code behind:

using System.Collections.Generic;

namespace WpfApplication1
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();

            List<DataItem> data = new List<DataItem>();
            data.Add(new DataItem() { Name = "AAA", IsValid = true });
            data.Add(new DataItem() { Name = "BBB" });
            DataContext = data;
        }

        public class DataItem
        {
            public string Name { get; set; }
            public bool IsValid { get; set; }
        }
    }
}
like image 168
Wallstreet Programmer Avatar answered Dec 12 '22 01:12

Wallstreet Programmer