处理来自ResourceDictionary的事件

时间:2018-05-07 11:14:32

标签: c# wpf xaml

我有8个自定义Radiobuttons样式,如下所示

<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事件?

1 个答案:

答案 0 :(得分:1)

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

  1. 在Visual Studio中添加一个新类,位于ResourceDictionary
  2. 的同一文件夹中
  3. 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">
    
  4. 现在将事件处理程序添加到TextBox txtM

  5. 有关详细信息,请查看Fredrik Hedblad's Answer