3

我想使用 aRadioButtonList并想检查RadioButtonList初始显示中的一个。

但是ListBox与主题一起使用时,radioButtons 无法正常工作。

我在 ViewModel 的构造函数中设置了“Radio1”toSelectedItem1 属性。但 Radio1 在初始显示时不会被检查。如果我单击 Radio2 并单击 Radio1,则 Radio1 已正确检查。

如果我从 app.xaml 中删除主题,则会在初始显示时正确检查 Radio1。我从http://wpf.codeplex.com/releases/view/14962下载了主题文件。

您可以通过https://github.com/koty/RadioButtonListTest下载该项目。

xml是:

<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>

视图模型是:

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>
4

1 回答 1

0

您必须更改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>
于 2012-11-19T17:26:50.340 回答