lundi 13 novembre 2017

Set CornerRadius of CheckBox in code behind

I have an application that allows multiple selection of items in the UI with checkboxes. The thing is checkboxes are generated from a file because the company adds or removes selections from the search.

Since I read a file to get all the possible selections available, the checkbox is generated in C# code, not XAML and one requirement for the UI is to have rounded checkboxes. How can I achieve this in C#?

I did this in XAML:

<CheckBox Content="QA Standard" IsEnabled="{Binding CanEdit, UpdateSourceTrigger=PropertyChanged}"
      ToolTip="{Binding StatusInfo, UpdateSourceTrigger=PropertyChanged}"
      BorderBrush="{Binding StatusColor, UpdateSourceTrigger=PropertyChanged}"
      BorderThickness="2"
      Margin="5" >
<CheckBox.Resources>
    <Style TargetType="{x:Type Border}">
        <Setter Property="CornerRadius" Value="12" />
    </Style>
</CheckBox.Resources>

Tried to redo it in C# but doesn't seems to work:

CheckBox c = new CheckBox() { BorderThickness = new Thickness(2), Content = b, Margin = new Thickness(5) };

var enabled = new Binding() { Source = DataContext, Path = new PropertyPath("CanEdit"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
var tooltip = new Binding() { Source = DataContext, Path = new PropertyPath("StatusInfo"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
var border = new Binding() { Source = DataContext, Path = new PropertyPath("StatusColor"), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };              

BindingOperations.SetBinding( c, CheckBox.IsEnabledProperty, enabled );
BindingOperations.SetBinding( c, CheckBox.ToolTipProperty, tooltip );
BindingOperations.SetBinding( c, CheckBox.BorderBrushProperty, border );

var cStyle = new Style( typeof(Border) );
cStyle.Setters.Add( new Setter( Border.CornerRadiusProperty, new CornerRadius( 12.0 ) ) );
c.Resources.Add( "CornerRadiusBorder", cStyle );

Boxes.Children.Add( c );




Aucun commentaire:

Enregistrer un commentaire