c# - 处理 ResourceDictionary 中的事件

标签 c# wpf xaml

我有 8 个自定义单选按钮,样式如下所示

<Style x:Key="RadioSubMenuTbox" TargetType="{x:Type RadioButton}">
    <Setter Property="Foreground" Value="#FFFFFF"/>
    <Setter Property="Height" Value="35"/>
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="FontFamily" Value="{StaticResource fontIbtisam}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                <Border Name="brdMenu" CornerRadius="0" Background="#20000000" BorderBrush="White" BorderThickness="0" Padding="1">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <ContentPresenter x:Name="RadioContentPresenter" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <ContentPresenter.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="TextAlignment" Value="Center" />
                                </Style>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                        <TextBox Name="txtM" Visibility="Collapsed" Margin="0,5,4,5" Style="{StaticResource txtboxDefaultNoShadow}" Grid.Column="1" Width="100"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="txtM" Property="Visibility" Value="Visible" />
                        <Setter TargetName="brdMenu" Property="Background" Value="#F2826A" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="brdMenu" Property="BorderBrush" Value="#F2826A"/>
                        <Setter TargetName="brdMenu" Property="TextElement.Foreground" Value="White"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

RadioButton IsChecked 时,RadioButton 中的文本框可见,现在样式位于 ResourceDictionary 文件中,我想处理每个文本框的 TextChanged 事件.

我可以按如下方式访问TextBox

TextBox aTBox = (TextBox)MyRButton.Template.FindName("txtM", MyContentControl);

但是如何处理 TextChanged 事件呢?

最佳答案

ResourceDictionary 可以像 Windows 一样有代码隐藏,您可以添加一个事件处理程序并从那里调用 textchanged,例如:

  1. 在 Visual Studio 中的 ResourceDictionary 同一文件夹中添加一个新类
  2. x:Class 属性添加到 XAML 文件

    <ResourceDictionary x:Class="YourNameSpace.YourClass"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
  3. 现在将事件处理程序添加到您的 TextBox txtM

更多详情可以查看Fredrik Hedblad's Answer

关于c# - 处理 ResourceDictionary 中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50212898/

相关文章:

c# - 如何以编程方式使用 SVN 和 .NET 进行文件版本控制?

c# - 将 dapper 与 .net 核心一起使用时,对象必须实现 IConvertible 异常

wpf - 在运行时更改 ListView 标题列高亮背景颜色

c# - Image ComboBox XAML 在运行时无法正确显示

c# - UWP 动画列表项移动

c# - UWP - 在 TextBlock 中垂直居中文本

Swift 中的 C# 阻塞集合

C# 控制台 : how can i read piped input from the command line

c# - 在 c# 中非阻塞等待/延迟一段时间的最佳实现是什么

c# - 如何将文本 block 动态添加到页面?