c# - WPF 我无法以我的风格访问文本块
问题描述
我制作了一个单选按钮样式,并将其命名为 RadioButtonStyle,但在我后面的代码中,当我编写单选按钮 Q1A 的名称并尝试 Q1A.RadioContentTxt 时,它不会出现。
看。这是我的风格:`
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SpeedReadingControl">
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="RadioButton.Static.Background" Color="#FFFFFFFF"/>
<SolidColorBrush x:Key="RadioButton.Static.Border" Color="#FFE4F0FF"/>
<Style x:Key="OptionMarkFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="RadioButton.MouseOver.Background" Color="#FFF3F9FF"/>
<SolidColorBrush x:Key="RadioButton.MouseOver.Border" Color="#FF5593FF"/>
<SolidColorBrush x:Key="RadioButton.MouseOver.Glyph" Color="#FF212121"/>
<SolidColorBrush x:Key="RadioButton.Disabled.Background" Color="#FFE6E6E6"/>
<SolidColorBrush x:Key="RadioButton.Disabled.Border" Color="#FFBCBCBC"/>
<SolidColorBrush x:Key="RadioButton.Disabled.Glyph" Color="#FF707070"/>
<SolidColorBrush x:Key="RadioButton.Pressed.Background" Color="#FFD9ECFF"/>
<SolidColorBrush x:Key="RadioButton.Pressed.Border" Color="#FF3C77DD"/>
<SolidColorBrush x:Key="RadioButton.Pressed.Glyph" Color="#FF212121"/>
<SolidColorBrush x:Key="RadioButton.Static.Glyph" Color="#FF212121"/>
<Style x:Key="RadioButtonStyle" TargetType="{x:Type RadioButton}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource RadioButton.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource RadioButton.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<ControlTemplate.Resources>
<Storyboard x:Key="OncheckSB">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Background).(SolidColorBrush.Color)" Storyboard.TargetName="RadioContentTxt">
<EasingColorKeyFrame KeyTime="0" Value="#FFE4F0FF"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="radioButtonBorder">
<EasingColorKeyFrame KeyTime="0:0:0.1" Value="#FF0075FF"/>
</ColorAnimationUsingKeyFrames>
<Int32AnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.RowSpan)" Storyboard.TargetName="radioButtonBorder">
<EasingInt32KeyFrame KeyTime="0:0:0.1" Value="2"/>
</Int32AnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="templateRoot" Background="Transparent" Height="14" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="radioButtonBorder" BorderBrush="AliceBlue" BorderThickness="{TemplateBinding BorderThickness}" Background="White" CornerRadius="100" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1,1,2,1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid x:Name="markGrid" Margin="4" >
<Canvas HorizontalAlignment="Left" >
<Path x:Name="CheckMarkIcon" Canvas.Left="-2" StrokeThickness="2" Stretch="Fill" Stroke="White" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Data="M427 391.5L429.5 394L435 389"/>
</Canvas>
<Ellipse x:Name="optionMark" Fill="White" MinWidth="6" MinHeight="6" Opacity="0"/>
</Grid>
</Border>
<TextBlock x:Name="RadioContentTxt" Grid.Column="1" Foreground="#33454B57" Margin="3,1,667.25,4562" TextWrapping="Wrap" Text="" Background="Transparent"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="OncheckSB_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard x:Name="OncheckSB_BeginStoryboard" Storyboard="{StaticResource OncheckSB}"/>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource OptionMarkFocusVisual}"/>
<Setter Property="Padding" Value="4,-1,0,0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.MouseOver.Border}"/>
<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.MouseOver.Glyph}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Disabled.Border}"/>
<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.Disabled.Glyph}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="radioButtonBorder" Value="{StaticResource RadioButton.Pressed.Border}"/>
<Setter Property="Fill" TargetName="optionMark" Value="{StaticResource RadioButton.Pressed.Glyph}"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Opacity" TargetName="optionMark" Value="1"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Opacity" TargetName="optionMark" Value="0.56"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我使用 Blend 制作它。
如何从后面的代码中设置我的 RadioContentTxt.Text?我尝试做 Q1A.Content 但它不起作用。
解决方案
TextBlock
将模板中的绑定到Content
控件的属性:
<TextBlock x:Name="RadioContentTxt" Grid.Column="1" Foreground="#33454B57" TextWrapping="Wrap"
Text="{TemplateBinding Content}" Background="Transparent"/>
...并设置Content
属性:
Q1A.Content = "...";
您不能使用控件的属性直接访问模板中的元素。
推荐阅读
- django - DoesNotExist at /accounts/login/ (delate example.com from admin page)
- javascript - 如何在 localStorage 中不断更新数据?
- javascript - 从远程托管页面访问 soket.io 服务器 - 需要未定义
- javascript - 使用 Bing Image API Javascript 搜索图像
- python - python替换数据框熊猫中所有列的多个值
- sql - 当 Rows 对应于其他列中的 MAX 时保持 NULL
- firebase - 我可以使用不同的参数并行运行自定义菜单中的谷歌应用程序脚本功能吗?
- javascript - FIrebase 应用程序未在 Vue3-Ionic 项目上初始化
- javascript - 检查 JS 远程替换、Shopify 中的变体可用性
- docker - 仅在发布阶段指定环境变量