我想使用RadioButtonList
并想在初始显示时检查RadioButtonList
之一。
但是ListBox
中的单选按钮无法与主题一起正确使用。
我在ViewModel的构造函数中将“Radio1”设置为SelectedItem1属性。但是在初始显示时不会检查Radio1。如果我单击Radio2并单击Radio1,则会正确检查Radio1。
如果我从app.xaml中删除了主题,则在初始显示中会正确检查Radio1。我从http://wpf.codeplex.com/releases/view/14962下载了主题文件。
您可以通过https://github.com/koty/RadioButtonListTest下载项目。
xaml是:
<Window x:Class="RadioButtonListTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RadioButtonListTest"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<ListBox ItemsSource="{Binding RBItems1}" SelectedItem="{Binding SelectedItem1}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton Content="{TemplateBinding Content}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Foreground="Black"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Window>
ViewModel是:
using System.Windows;
namespace RadioButtonListTest
{
public class MainWindowViewModel : DependencyObject
{
public MainWindowViewModel()
{
this.RBItems1 = new[] { "Radio1", "Radio2" };
this.SelectedItem1 = "Radio1";
}
public static readonly DependencyProperty RBItems1Property
= DependencyProperty.Register("RBItems1",
typeof (string[]),
typeof (MainWindowViewModel),
new PropertyMetadata(default(string[])));
public static readonly DependencyProperty SelectedItem1Property
= DependencyProperty.Register("SelectedItem1",
typeof (string),
typeof (MainWindowViewModel),
new PropertyMetadata(default(string)));
public string[] RBItems1
{
get { return (string[]) GetValue(RBItems1Property); }
set { SetValue(RBItems1Property, value); }
}
public string SelectedItem1
{
get { return (string)GetValue(SelectedItem1Property); }
set { SetValue(SelectedItem1Property, value); }
}
}
}
而app.xaml是:
<Application x:Class="RadioButtonListTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="Themes\ShinyBlue\Theme.xaml"/>
</Application.Resources>
</Application>
最佳答案
您必须在 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>