C# 列表框中的单选按钮不';不能正确使用主题
我想使用C# 列表框中的单选按钮不';不能正确使用主题,c#,wpf,mvvm,radio-button,themes,C#,Wpf,Mvvm,Radio Button,Themes,我想使用RadioButtonList并想在初始显示时检查RadioButtonList之一 但是列表框中的单选按钮在使用with theme时不能正常工作 我在ViewModel的构造函数中将“Radio1”设置为selecteditem1属性。但Radio1在初始显示时不会被检查。如果单击Radio2并单击Radio1,则Radio1已正确选中 如果我从app.xaml中删除主题,Radio1将在初始显示时正确检查。我从下载了主题文件 您可以通过下载该项目 xaml是: ViewMode
RadioButtonList
并想在初始显示时检查RadioButtonList
之一
但是列表框中的单选按钮在使用with theme时不能正常工作
我在ViewModel的构造函数中将“Radio1”设置为selecteditem1属性。但Radio1在初始显示时不会被检查。如果单击Radio2并单击Radio1,则Radio1已正确选中
如果我从app.xaml中删除主题,Radio1将在初始显示时正确检查。我从下载了主题文件
您可以通过下载该项目
xaml是:
ViewModel是:
使用System.Windows;
命名空间RadioButtonListTest
{
公共类MainWindowViewModel:DependencyObject
{
公共主窗口视图模型()
{
this.RBItems1=new[]{“Radio1”,“Radio2”};
this.SelectedItem1=“Radio1”;
}
公共静态只读从属属性RBItems1Property
=DependencyProperty.Register(“RBItems1”,
typeof(字符串[]),
类型(MainWindowViewModel),
新的PropertyMetadata(默认值(字符串[]));
公共静态只读从属属性SelectedItem1属性
=DependencyProperty.Register(“SelectedItem1”,
类型(字符串),
类型(MainWindowViewModel),
新属性元数据(默认值(字符串));
公共字符串[]RBItems1
{
获取{return(string[])GetValue(RBItems1Property);}
set{SetValue(rbitems1属性,值);}
}
公共字符串SelectedItem1
{
获取{return(string)GetValue(SelectedItem1Property);}
set{SetValue(SelectedItem1Property,value);}
}
}
}
app.xaml是:
您必须在ShinyBlue.xaml中更改一点代码
在原始代码中没有一个触发器
查找RadioButtonTemplate
,并使用以下内容更改它:
<ControlTemplate x:Key="RadioButtonTemplate" TargetType="{x:Type RadioButton}">
<ControlTemplate.Resources>
<Storyboard x:Key="CheckedFalse">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="CheckedTrue">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="CheckIcon" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HoverOn">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundOverlay" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HoverOff">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="BackgroundOverlay" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="PressedOn">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="0.6"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="PressedOff">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Background" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Grid Width="16" Height="16">
<Ellipse Height="14"
Margin="1"
x:Name="Background"
Width="14"
Fill="{StaticResource NormalBrush}"
Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="2"/>
<Ellipse Height="12"
x:Name="BackgroundOverlay"
Width="12"
Opacity="0"
Fill="{StaticResource HoverBrush}"
Stroke="#00000000"
Margin="2,2,2,2"
StrokeThickness="0"/>
<Border HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="6"
Height="6"
CornerRadius="1,1,1,1"
BorderThickness="1,1,1,1"
Background="#FFFFFFFF"
x:Name="CheckIcon"
BorderBrush="{StaticResource CheckIconBrush}"
Opacity="0"/>
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource CheckedFalse}" x:Name="CheckedFalse_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource CheckedTrue}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource CheckedTrue}" x:Name="CheckedTrue_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource CheckedFalse}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource HoverOn}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource HoverOff}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource PressedOn}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource PressedOff}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Fill" TargetName="Background" Value="{DynamicResource DisabledBackgroundBrush}"/>
<Setter Property="Stroke" TargetName="Background" Value="{DynamicResource DisabledBorderBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这是MVVM完全错误的方法,您是从DependencyObject继承VM。您应该改为创建UserControl。使用您的代码,单选按钮工作正常。非常感谢你!