我有一个应用程序,允许在UI中选择多个具有复选框的项目。事情是从文件生成复选框,因为公司在搜索中添加或删除选择。
由于我读取了一个文件以获取所有可用的选项,因此复选框是在C#代码中生成的,而不是XAML,并且UI的一个要求是具有舍入的复选框。我怎样才能在C#中实现这个目标?
我是在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>
试图用C#重做它,但似乎不起作用:
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 );
答案 0 :(得分:1)
只需更改资源的密钥:
var cStyle = new Style(typeof(Border));
cStyle.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(12.0)));
c.Resources.Add(typeof(Border), cStyle);