单选按钮和复选框将动态添加到窗口,该窗口基于数据库中的数据量。
我尝试了一些方法,但无法获得所需的东西。下面是添加单选按钮或复选框所执行的代码:-
private void ScreenSubList_Loaded(object sender, RoutedEventArgs e)
{
try
{
strSubList LastGroupName = strSubList.Empty;
foreach (var SubList in ProductSubList)
{
StackPanel StackGroup = new StackPanel() { Orientation = Orientation.Vertical };
if (SubList.GroupName != LastGroupName)
{
Label LabelGroupName = new Label() { Content = SubList.GroupName.ToUpper() };
ScaleTransform ElementScaleTransform = new ScaleTransform(6, 6);
LabelGroupName.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(LabelGroupName);
LastGroupName = SubList.GroupName;
}
if (SubList.GroupType == 0)
{
RadioButton rb = new RadioButton();
if (SubList.SubListItem != null)
{
StackPanel StackItem = new StackPanel() { Orientation = Orientation.Horizontal };
foreach (var SubListitem in SubList.SubListItem)
{
rb.Tag = SubListitem.ItemID;
rb.Name = "SubList" + SubListitem.ItemID;
rb.Content = SubListitem.ItemName;
rb.HorizontalContentAlignment = HorizontalAlignment.Left;
rb.VerticalContentAlignment = VerticalAlignment.Center;
rb.GroupName = SubList.GroupName;
ScaleTransform ElementScaleTransform = new ScaleTransform(5, 5);
rb.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(rb);
}
}
}
else if (SubList.GroupType == 1)
{
CheckBox cbx = new CheckBox();
if (SubList.SubListItem != null)
{
StackPanel StackItem = new StackPanel() { Orientation = Orientation.Horizontal };
foreach (var SubListitem in SubList.SubListItem)
{
cbx.Tag = SubListitem.ItemID;
cbx.Name = "SubList" + SubListitem.ItemID;
cbx.Content = SubListitem.ItemName;
cbx.HorizontalContentAlignment = HorizontalAlignment.Left;
cbx.VerticalContentAlignment = VerticalAlignment.Center;
ScaleTransform ElementScaleTransform = new ScaleTransform(5, 5);
cbx.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(cbx);
}
}
}
ScreenSubListredient.StackSubList.Children.Add(StackGroup);
}
}
catch (Exception ex)
{
App.LogEvents($"Exception on ScreenSubList_Loaded. Message-{ex.Message}. Stack Trace-{ex.StackTrace}", System.Diagnostics.EventLogEntryType.Error);
}
}
我还与Blend一起玩耍,以查看测试结果。一些测试是: 1.在添加到堆栈面板之前,缩放转换单选按钮和复选框 2.将默认的单选按钮和复选框分组到视图中。
测试问题:
在最左侧,我只是更改了文本大小,文本变大了,但项目符号选项仍然很小。
在中间,使用Viewbox的选项。所有字体均为默认大小Segoe UI 9pt
最右边是ScaleTransform的用法。中间指针可能垂直堆积在面板上。不确定如何从单选按钮和复选框的最新大小控制基数,因为在“高度和宽度”属性上,它会显示“变换”之前的默认大小。
实际上,我需要的是一个单选按钮和一个带有项目符号的复选框,其大小与文本的大小相同。我已经上网了一个星期,但没有找到适合我的情况的解决方案。
答案 0 :(得分:1)
RadioButton和CheckBox在其模板中具有硬编码的项目符号大小。我已采用RadioButton template和CheckBox template并修改了子弹大小。我将它们的高度和宽度绑定到内容ActualHeight:
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}"
完整的模板:
<Style x:Key="{x:Type RadioButton}" TargetType="{x:Type RadioButton}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource RadioButtonFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Grid Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}" >
<Ellipse x:Name="Border"
Fill="{StaticResource NormalBrush}"
StrokeThickness="1"
Stroke="{StaticResource NormalBorderBrush}" />
<Ellipse x:Name="CheckMark"
Margin="4"
Fill="{StaticResource GlyphBrush}" />
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter Name="PART_Content"
Margin="4,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource DarkBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="Stroke" Value="{StaticResource GlyphBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="Stroke" Value="#40000000" />
<Setter Property="Foreground" Value="#80000000"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
<Setter Property="IsThreeState" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckBoxFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Border x:Name="Border"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}"
CornerRadius="0"
Background="{StaticResource NormalBrush}"
BorderThickness="1"
BorderBrush="{StaticResource NormalBorderBrush}">
<Viewbox Margin="2">
<Path
Width="7" Height="7"
x:Name="CheckMark"
StrokeStartLineCap="Flat"
StrokeEndLineCap="Flat"
SnapsToDevicePixels="False"
Stroke="{StaticResource GlyphBrush}"
StrokeThickness="2"
Data="M 1 1 L 6 6 M 1 6 L 6 1" />
</Viewbox>
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="4,0,0,0" Name="PART_Content"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="CheckMark" Property="Data" Value="M 3 3 L 3 4 L 4 4 L 4 3 Z" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>