【问题标题】:Set property 'System.Windows.Controls.Primitives.ToggleButton.IsChecked' threw an exception设置属性“System.Windows.Controls.Primitives.ToggleButton.IsChecked”引发异常
【发布时间】:2020-11-30 19:08:59
【问题描述】:

我正在尝试使用 Application Resources 为 ToggleButton 添加自定义样式,当我将样式调用为 StaticResource 并将其添加到属性为 IsChecked="True" 的 ToggleButton 控件时,出现此错误。

抛出异常:PresentationFramework.dll 中的“System.Windows.Markup.XamlParseException”
'设置属性 'System.Windows.Controls.Primitives.ToggleButton.IsChecked' 引发异常。'

InvalidOperationException:在“System.Windows.Controls.ControlTemplate”的名称范围内找不到“Border”名称。

App.xaml:

<Style x:Key="myToggleSwitch" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Viewbox>
                    <Grid x:Name="toggleSwitch">
                        <Border x:Name="Border" CornerRadius="10"
                Background="#FFFFFFFF"
                Width="80" Height="25">
                            <Border.Effect>
                                <DropShadowEffect ShadowDepth="0.5" Direction="0" Opacity="0.3" />
                            </Border.Effect>
                            <Ellipse x:Name="Ellipse" Fill="#FFFFFFFF" Stretch="Uniform"
                     Margin="2 2 2 1"
                     Stroke="Gray" StrokeThickness="0.2"
                     HorizontalAlignment="Left" Width="22" >
                                <Ellipse.Effect>
                                    <DropShadowEffect BlurRadius="10" ShadowDepth="1" Opacity="0.3" Direction="260" />
                                </Ellipse.Effect>
                            </Ellipse>
                        </Border>

                        <TextBlock x:Name="txtOff" Text="OFF" Margin="0 0 8 0" VerticalAlignment="Center" FontWeight="DemiBold" HorizontalAlignment="Right" Foreground="White"/>
                        <TextBlock x:Name="txtOn" Text="ON" Margin="15 0 0 0" VerticalAlignment="Center" FontWeight="DemiBold"  Foreground="White" HorizontalAlignment="Left"/>

                    </Grid>
                </Viewbox>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Checked">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="Border"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#34A543"
                                        Duration="0:0:0.1" />

                                <ThicknessAnimation Storyboard.TargetName="Ellipse"
                                            Storyboard.TargetProperty="Margin"
                                            To="56 2 2 1"
                                            Duration="0:0:0.1" />
                                <DoubleAnimation
                                    Storyboard.TargetName="txtOff" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:0:0.1"     />
                                <DoubleAnimation
                                    Storyboard.TargetName="txtOn" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="0.0" To="1.0" Duration="0:0:0:0.1"  />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Unchecked">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetName="Border"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#C2283B"
                                        Duration="0:0:0.1" />
                                <ThicknessAnimation Storyboard.TargetName="Ellipse"
                                            Storyboard.TargetProperty="Margin"
                                            To="2 2 2 1"
                                            Duration="0:0:0.1" />
                                <DoubleAnimation
                                    Storyboard.TargetName="txtOff" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="0" To="1.0" Duration="0:0:0:0.1"       />
                                <DoubleAnimation
                                    Storyboard.TargetName="txtOn" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

来源:Link

MainWindow.xaml:

<Window>
    <Grid>
        <ToggleButton x:Name="tog" Style="{StaticResource myToggleSwitch}" IsChecked="True" Checked="ToggleButton_Checked"></ToggleButton>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

我该如何解决这个问题?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    它将按如下方式工作。使用Trigger.EnterActions 而不是EventTrigger 解决了这个问题。

    <Style x:Key="myToggleSwitch" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Grid x:Name="toggleSwitch">
                            <Border x:Name="Border" CornerRadius="10"
                Background="#FFFFFFFF"
                Width="80" Height="25">
                                <Border.Effect>
                                    <DropShadowEffect ShadowDepth="0.5" Direction="0" Opacity="0.3" />
                                </Border.Effect>
                                <Ellipse x:Name="Ellipse" Fill="#FFFFFFFF" Stretch="Uniform"
                     Margin="2 2 2 1"
                     Stroke="Gray" StrokeThickness="0.2"
                     HorizontalAlignment="Left" Width="22" >
                                    <Ellipse.Effect>
                                        <DropShadowEffect BlurRadius="10" ShadowDepth="1" Opacity="0.3" Direction="260" />
                                    </Ellipse.Effect>
                                </Ellipse>
                            </Border>
    
                            <TextBlock x:Name="txtOff" Text="OFF" Margin="0 0 25 0" VerticalAlignment="Center" FontWeight="DemiBold" HorizontalAlignment="Right" Foreground="White"/>
                            <TextBlock x:Name="txtOn" Text="ON" Margin="25 0 0 0" VerticalAlignment="Center" FontWeight="DemiBold"  Foreground="White" HorizontalAlignment="Left"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="Border"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#34A543"
                                        Duration="0:0:0.1" />
    
                                            <ThicknessAnimation Storyboard.TargetName="Ellipse"
                                            Storyboard.TargetProperty="Margin"
                                            To="56 2 2 1"
                                            Duration="0:0:0.1" />
                                            <DoubleAnimation
                                    Storyboard.TargetName="txtOff" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:0:0.1"     />
                                            <DoubleAnimation
                                    Storyboard.TargetName="txtOn" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="0.0" To="1.0" Duration="0:0:0:0.1"  />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                                <!--  some out fading  -->
                                <Trigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetName="Border"
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="#C2283B"
                                        Duration="0:0:0.1" />
                                            <ThicknessAnimation Storyboard.TargetName="Ellipse"
                                            Storyboard.TargetProperty="Margin"
                                            To="2 2 2 1"
                                            Duration="0:0:0.1" />
                                            <DoubleAnimation
                                    Storyboard.TargetName="txtOff" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="0" To="1.0" Duration="0:0:0:0.1"       />
                                            <DoubleAnimation
                                    Storyboard.TargetName="txtOn" 
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:0:0.1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.ExitActions>
                                <Setter Property="Foreground" Value="{DynamicResource IdealForegroundColorBrush}" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="False">
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{DynamicResource GrayBrush7}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Style>
    

    【讨论】:

    • 谢谢,这个解决方案对我有用,虽然文本不再出现,但现在异常消失了
    • 如果您的意思是从文本中打开关闭,这是由于边距设置所致。由于没时间,我赶紧调整了一下,大家可以好好修一下。
    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    相关资源
    最近更新 更多