1

我正在尝试对表单中的所有边框应用以下样式:

  <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>
         ...

我在这里缺少什么?

4

3 回答 3

1

我为您的问题编写了两个样式代码。您可以使用 Style.Resources 或 ControlTemplate.Resources。此外,您找到了正确的解决方案

这是代码:(使用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>
于 2014-11-12T07:24:53.810 回答
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>
于 2014-11-11T08:26:45.670 回答
0

我通过将边框样式分配给该样式解决了这个问题,该样式<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>
于 2014-11-12T02:24:39.290 回答