代码之家  ›  专栏  ›  技术社区  ›  RezaNoei

无法访问嵌套WPF样式上的样式[关闭]

  •  -1
  • RezaNoei  · 技术社区  · 7 年前

     <Style TargetType="UserControl" x:Key="ViewForm">
         <Setter Property="Padding" Value="18"></Setter>
         <Style.Resources>
             <Style TargetType="TextBlock">
                 <Setter Property="FontFamily" Value="{StaticResource IranSans}"/>
                 // ...
             </Style>
    
             <Style TargetType="TextBlock" x:Key="Right">
                 <Setter Property="FontFamily" Value="{StaticResource IranSans}"/>
             </Style>
    
             <Style TargetType="TextBlock" x:Key="Left">
                 <Setter Property="FontFamily" Value="{StaticResource IranSans}"></Setter>
                 // ...
             </Style>
    
             <Style TargetType="Label">
                 <Setter Property="FontFamily" Value="{StaticResource IranSans}"/>
                 // ...
             </Style>
         </Style.Resources>
     </Style>
    

    我把它用在我的 UserControls 我希望能用 Left Right 我的风格 TextBlock 但我无法接近他们。例如:

     <TextBlock Grid.Row="3" Style="{StaticResource Right}">Email :</TextBlock>
    

    ***那些没有 x:Key="SomeKey..."

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shashi Bhushan    7 年前

    用户控件“资源”和用户控件“样式资源”是两个不同的东西。用户控件的嵌套控件(如文本框、标签)将访问“资源”,而不是“样式资源”。

    所以正确的做法是-

    <UserControl>
    <UserControl.Resources>
      <!-- Place all styles that you want to put over child controls -->
    </UserControl.Resources>
    <UserControl.Style>
      <Style>
            <Style.Resources>
                 <!-- Place all styles that you want to use within this style -->
            </Style.Resources>
       </Style>
    </UserControl.Style>
    </UserControl>
    
    推荐文章