我在 GridView(A) 中有一个 GridView(B)。GridView-A 的样式与 GridView-B 的样式不同,但是当我的 GridView-B 继承了 GridView-A 的所有样式时,就会出现问题。
我不希望这种情况发生。
他们有什么办法,我只能在 xaml 中实现吗?
我在 GridView(A) 中有一个 GridView(B)。GridView-A 的样式与 GridView-B 的样式不同,但是当我的 GridView-B 继承了 GridView-A 的所有样式时,就会出现问题。
我不希望这种情况发生。
他们有什么办法,我只能在 xaml 中实现吗?
只需按照以下步骤操作,如果您对此有任何问题,请告诉我。其中你可能已经意识到了。可能是你错过了什么。
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>
您也可以在后面的代码中添加这两种样式。希望能帮助到你!!!
您应该为两个网格视图明确定义您的样式。在WPF中子控件继承父控件的属性,可能是你有这个问题。您可能为 gridviewA 而不是为 gridviewB 定义了样式,或者您对所有 gridviews 使用相同的样式。如果你可以分享代码,我可以告诉你到底是什么问题。