嘿,我正在尝试在WPF中设置按钮样式.我有一个基本风格:
<Style x:Key="buttonStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="ButtonBaseControlTemplate" TargetType="{x:Type ButtonBase}">
<Grid SnapsToDevicePixels="True">
<Border x:Name="Bd" Background="#9BC4E2" BorderBrush="Black"
BorderThickness="3,3,3,3" CornerRadius="7,7,7,7" Padding="5,5,5,5">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="Content"
ContentSource="Content"
RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
<-- Removed triggers here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
但我也需要使用它来切换按钮,只是为了改变IsChecked背景颜色.但是,当我尝试这样时,我无法访问边框来设置背景:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}">
</Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource buttonStyle}">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="White"/> <-- Need to specify target name of the border here somehow -->
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
有什么建议?TA
我有一个关于WPF中的跨线程调用的问题.
foreach (RadioButton r in StatusButtonList)
{
StatusType status = null;
r.Dispatcher.Invoke(new ThreadStart(() => status= ((StatusButtonProperties)r.Tag).StatusInformation));
if (AppLogic.CurrentStatus == null || AppLogic.CurrentStatus.IsStatusNextLogical(status.Code))
{
SolidColorBrush green = new SolidColorBrush(Color.FromRgb(102, 255, 102));
r.Dispatcher.Invoke(new ThreadStart(() => r.Background = green));
}
else
{
SolidColorBrush red = new SolidColorBrush(Color.FromRgb(255, 0, 0));
r.Dispatcher.Invoke(new ThreadStart(() => r.Background = red));
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,它在第一次迭代时正常工作.但是在第二次迭代期间,该行:
r.Dispatcher.Invoke(new ThreadStart(() => status= ((StatusButtonProperties)r.Tag).StatusInformation))
Run Code Online (Sandbox Code Playgroud)
导致此异常:
Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.
Run Code Online (Sandbox Code Playgroud)
我尝试了一些解决方案,但我找不到任何可行的方法.
任何帮助赞赏!