【问题标题】:Styling for Grid inside Another Grid另一个网格内的网格样式
【发布时间】:2017-05-30 12:31:56
【问题描述】:

我在 GridView(A) 中有一个 GridView(B)。 GridView-A 的样式与 GridView-B 的样式不同,但是当我的 GridView-B 继承了 GridView-A 的所有样式时,就会出现问题。

我不希望这样的事情发生。

他们有什么办法吗,我只能在 xaml 中实现?

【问题讨论】:

  • 你有代码要显示吗?这样只有别人可以帮助你
  • 我不认为 GridView B 继承了 A 的风格。您可能已经指定了所有网格视图都将使用的通用样式。 Gridview 在 Gridview B 之外有不同的样式吗? (这变得令人困惑)
  • (当然,您可以为 GridView B 指定特定样式并转到<GridView Id="GridViewB" Style="{StaticResource bGridViewStyle}"></GridView>
  • 请告诉我们您的 GridView 是如何定义的。

标签: c# wpf xaml c#-4.0 xaml-designer


【解决方案1】:

只需按照以下步骤操作,如果您对此有任何问题,请告诉我。其中你可能已经意识到了。可能是你错过了什么。

1) 添加 ResourceDictionary 作为样式表。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:XYZApplication">
 <!-- Style 1-->
  <Style x:Key="GridStyle1" TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="BlanchedAlmond"></Setter>
        <Setter Property="Margin" Value="1,1,1,1"></Setter>
    </Style>
<!-- Style 2-->
    <Style x:Key="GridStyle2" TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="Aqua"></Setter>
        <Setter Property="Margin" Value="5,5,5,5"></Setter>
    </Style>
</ResourceDictionary>

2) 将资源文件添加到您的用户控件/窗口中,如下所示...

<UserControl x:Class="XYZApplication.Views.Payment"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:XYZApplication.Views"
             mc:Ignorable="d">

 <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../Style/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
</UserControl>

3) 将这些特定样式用于用户控件/窗口内的指定网格

 <Grid Style="{StaticResource GridStyle1}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
       <Grid Grid.Column="1" Grid.Row="1"  Style="{StaticResource GridStyle2}" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
        <Label  Grid.Column="0" Grid.Row="0">Bank Name</Label>
            <Label  Grid.Column="1" Grid.Row="0">Branch</Label>
            <Label  Grid.Column="2" Grid.Row="0">Account holder</Label>
            <Label  Grid.Column="3" Grid.Row="0">Account Number</Label>
            <Label  Grid.Column="4" Grid.Row="0">Balance</Label>
            <Label  Grid.Column="0" Grid.Row="1">Bank of ...</Label>
            <Label  Grid.Column="1" Grid.Row="1">...Branch</Label>
            <Label  Grid.Column="2" Grid.Row="1">My Name</Label>
            <Label  Grid.Column="3" Grid.Row="1">Account Number 123</Label>
            <Label  Grid.Column="4" Grid.Row="1">1 billion</Label>
        </Grid>
    </Grid>

您也可以在后面的代码中添加这两种样式。 希望对你有帮助!!!

【讨论】:

  • 感谢您的帮助。
【解决方案2】:

您应该为两个网格视图明确定义您的样式。在WPF中子控件继承父控件的属性,可能是你有这个问题。您可能已经为 gridviewA 而不是为 gridviewB 定义了样式,或者您对所有 gridviews 使用了相同的样式。如果你能分享代码,我可以告诉你到底是什么问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2020-04-03
    相关资源
    最近更新 更多