question

929Free-0494 avatar image
0 Votes"
929Free-0494 asked 929Free-0494 commented

WPF DataGridCheckBoxColumn Control question

Hello, I need modify DataGridCheckBoxColumn Control inside checkbox size, but i don't find a way to modify it, Who can tell me ? Thanks

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

gekka avatar image
0 Votes"
gekka answered 929Free-0494 commented

CheckBoxes can be changed by setting the ElementStyle property.

<DataGrid DataContext="ABC" ItemsSource="{Binding}" >
    <DataGrid.Columns >
                
        <DataGridCheckBoxColumn  Header="CheckBox">
            <DataGridCheckBoxColumn.ElementStyle>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <ScaleTransform ScaleX="2" ScaleY="2" />
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridCheckBoxColumn.ElementStyle>
        </DataGridCheckBoxColumn>
                
    </DataGrid.Columns>
</DataGrid>
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, Thanks, this is XAML, Is there a way to write "C#" ?
My Code:

         DataGridCheckBoxColumn optionCheck = new DataGridCheckBoxColumn();
         optionCheck.IsThreeState = false;

         DG1.Columns.Add(optionCheck);

thanks

0 Votes 0 ·
DataGridCheckBoxColumn optionCheck = new DataGridCheckBoxColumn();
optionCheck.IsThreeState = false;

Setter setter = new Setter();
setter.Property = FrameworkElement.LayoutTransformProperty;
setter.Value = new ScaleTransform(2, 2);

Style style = new Style(typeof(CheckBox));
style.Setters.Add(setter);
optionCheck.ElementStyle = style;

DG.Columns.Add(optionCheck);
1 Vote 1 ·

yes, thanks very much. This solution is very good!

0 Votes 0 ·
PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered 929Free-0494 commented

Hi, try this MVVM demo to add column by code. Include System.Windows.Interactivity from Nuget.

 <Window x:Class="WpfApp1.Window49"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:WpfApp49"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         mc:Ignorable="d"
         Title="Window49" Height="450" Width="800">
   <Window.DataContext>
     <local:ViewModel/>
   </Window.DataContext>
     <Grid>
     <DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False">
       <i:Interaction.Behaviors>
         <local:DataGridBehavior/>
       </i:Interaction.Behaviors>
     </DataGrid>
     </Grid>
 </Window>

 using System.Collections.Generic;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Interactivity;
 using System.Windows.Media;
    
 namespace WpfApp49
 {
   public class ViewModel
   {
     public List<Data> View { get => GetData(); }
     private List<Data> GetData()
     {
       var l = new List<Data>();
       for (int i = 0; i < 10; i++) l.Add(new Data() { ID = i });
       return l;
     }
    
     public class Data
     {
       public int ID { get; set; }
       public bool Switch { get; set; } = true;
     }
   }
   public class DataGridBehavior : Behavior<DataGrid>
   {
     protected override void OnAttached()
     {
       AssociatedObject.Columns.Add(new DataGridTextColumn() { Header = "ID", Binding = new Binding("ID") });
       var dgcbc = new DataGridCheckBoxColumn() { Header = "Switch", Binding = new Binding("Switch") };
       AssociatedObject.Columns.Add(dgcbc);
       var st = new Style() { TargetType = typeof(DataGridCell) };
       st.Setters.Add(new Setter() { Property = DataGridCell.LayoutTransformProperty, Value = new ScaleTransform() { ScaleX = 2, ScaleY = 2 } });
       dgcbc.CellStyle = st;
     }
   }
 }

10300-x.png



x.png (21.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.