Почему элемент управления не выбирает общий стиль, определенный для элементов управления его типа

1

Я пытаюсь применить следующий стиль для всех границ s в моей форме:

  <UserControl.Resources>

    <Style TargetType="{x:Type Border}">
      <Setter Property="BorderBrush" Value="#5076A7" />
      <Setter Property="BorderThickness" Value="1" />
      <Setter Property="CornerRadius" Value="4" />
    </Style>

    <Style ... />

  <UserControl.Resources>

Тем не менее моя граница внутри моего списка ListView, а также не других границ, поднимает этот стиль, если я не использую значение x:Key FooSyle и не ссылаюсь на ключ в моем <Boder Style={StaticResource FooStyle}> который, если точно не то, что я хочу делать.

Граница упоминается ниже:

   <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
      <Style.Setters>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
              <Border>
                <Grid Margin="2">
                  <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                  </Grid.RowDefinitions>
         ...

Что мне здесь не хватает?

  • 0
    Это встроено в ControlTemplate, поэтому. Вы можете либо объявить это явно, как вы говорили с Style =, либо вы можете добавить это в шаблон элемента управления или объявление BasedOn, указывающее на шаблон.
  • 0
    @ChrisW. для второго предложения, будет ли BasedOn нуждаться в том же типе? а также Является ли встраивание вообще проблемой или только ControlTemplate имеет такую природу? Просто хочу понять это немного больше.
Показать ещё 1 комментарий
Теги:
xaml
wpf
styles
wpf-style

3 ответа

0
Лучший ответ

Я решил проблему, выделив стиль Border Style для этого стиля <Style.Resources> который описывается как концепция вложенных стилей, такая как:

   <ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
      <Style.Setters>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
              <Border>
                <Grid Margin="2">
                  <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                  </Grid.RowDefinitions>

                ...
                </Grid>
              </Border>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style.Setters>
      <!--Nested Style-->
      <Style.Resources>
            <Style TargetType="{x:Type Border}">
              <Setter Property="CornerRadius" Value="1" />
            </Style>
            <Style TargetType="{x:Type TextBox}">
              <Setter Property="Background" Value="Transparent" />
              <Setter Property="Foreground" Value="White" />
            </Style>
      </Style.Resources>
    </Style>
  </ListView.ItemContainerStyle>
1

Я написал два стиля кода для вашей проблемы. Вы можете использовать Style.Resources или ControlTemplate.Resources.Also, вы нашли правильное решение

Вот код: (используя DynamicResource)

 <Window.Resources>
    <Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="BorderBrush" Value="#5076A7" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="CornerRadius" Value="4" />
    </Style>
</Window.Resources>

 <ListView>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Border Height="100" Style="{DynamicResource ListViewItemBorderStyle}">
                                <!--....-->
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Еще один код (с использованием StaticResource). Он встроен в ControlTemplate

 <ListView>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <ControlTemplate.Resources>
                                <Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
                                    <Setter Property="Background" Value="Red"/>
                                    <Setter Property="BorderBrush" Value="#5076A7" />
                                    <Setter Property="BorderThickness" Value="1" />
                                    <Setter Property="CornerRadius" Value="4" />
                                </Style>
                            </ControlTemplate.Resources>
                            <Border Height="100" Style="{DynamicResource ListViewItemBorderStyle}">
                                <!--....-->
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
0

Вы можете добавить BorderStyle в ControlTemplate.Resources

Вот код:

  <ListView>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListViewItem Content="asdasd"/>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <ControlTemplate.Resources>
                                <Style x:Key="ListViewItemBorderStyle" TargetType="{x:Type Border}">
                                    <Setter Property="Background" Value="Red"/>
                                    <Setter Property="BorderBrush" Value="#5076A7" />
                                    <Setter Property="BorderThickness" Value="1" />
                                    <Setter Property="CornerRadius" Value="4" />
                                </Style>
                            </ControlTemplate.Resources>
                            <Border Height="100" Style="{StaticResource ListViewItemBorderStyle}">
                             <!--....-->
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
  • 0
    Спасибо, но почему бы не в <Style.Resource> ?

Ещё вопросы

Сообщество Overcoder
Наверх
Меню