猿问

在触发器 wpf 中无法设置样式

我想在选中切换按钮时更改样式


<ToggleButton.Style>

                <Style TargetType="ToggleButton" BasedOn="{StaticResource ToggleButtonPrimary}">

                    <Style.Triggers>


                        <DataTrigger Binding="{Binding ElementName=ButtonNude, Path=IsChecked}" Value="True">

                            <Setter Property="Style" Value="{StaticResource ToggleButtonDanger}"/>

                        </DataTrigger>

                    </Style.Triggers>

                </Style>

            </ToggleButton.Style>

但我的代码不起作用,应用程序崩溃


慕虎7371278
浏览 167回答 1
1回答

慕村9548890

正确的方法是使用模板并应用controlTemplateTargetName还有一种(可能是丑陋的)方法:在视图:&nbsp; &nbsp; <StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel.Resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <local:Myconverter x:Key="MyConverter" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="ToggleButton" x:Key="ToggleButtonPrimary">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="blue" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="ToggleButton" x:Key="ToggleButtonDanger">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="Red" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel.Resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox Margin="20" x:Name="chk" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ToggleButton Width="100" Height="100" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ToggleButton.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <MultiBinding Converter="{StaticResource MyConverter}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <MultiBinding.Bindings>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Binding ElementName="chk" Path="IsChecked"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Binding RelativeSource="{RelativeSource AncestorType=StackPanel}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </MultiBinding.Bindings>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </MultiBinding>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ToggleButton.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ToggleButton>&nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>转炉:&nbsp; &nbsp; public class Myconverter : IMultiValueConverter&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((bool)values[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (values[1] as FrameworkElement).Resources["ToggleButtonDanger"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (values[1] as FrameworkElement).Resources["ToggleButtonPrimary"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答