Wpf 是否有一个XAML等价于CSS';网格面积是多少?
WPF XAML是否与CSS具有等效性?即,创建Wpf 是否有一个XAML等价于CSS';网格面积是多少?,wpf,xaml,Wpf,Xaml,WPF XAML是否与CSS具有等效性?即,创建行,列,行,列值定义的方法,给该定义一个标识符,然后通过标识符使用这些值 我在想象这样的事情: <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition /> <RowDefinition Height="auto" /> </Grid.RowDefiniti
行
,列
,行
,列
值定义的方法,给该定义一个标识符,然后通过标识符使用这些值
我在想象这样的事情:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.AreaDefinitions>
<AreaDefinition Row="0" Column="0" ColumnSpan="2" Name="Header" />
<AreaDefinition Row="1" Column="0" Name="Navigation" />
<AreaDefinition Row="1" Column="1" Name="Main" />
<AreaDefinition Row="2" Column="0" ColumnSpan="2" Name="Footer" />
</Grid.AreaDefinitions>
<TextBlock Grid.Area="Header" Text="Header" />
<TextBlock Grid.Area="Navigation" Text="Navigation" />
<TextBlock Grid.Area="Main" Text="Main" />
<TextBlock Grid.Area="Footer" Text="Footer" />
</grid>
没有完全类似的功能,但是有一个功能可以类似地使用。是“将一组属性值应用于多个元素的方便方法”。如果有许多元素都需要将相同的属性设置为相同的值,则可以定义
样式,如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style x:Key="AStyle" TargetType="TextBlock">
<Setter Property="Grid.Row" Value="1"/>
<Setter Property="Grid.Column" Value="1"/>
</Style>
</Grid.Resources>
<TextBlock Style="{StaticResource AStyle}" Text="Header" />
</Grid>
在您的示例代码中,这将是无用的,因为没有一个TextBlock
s共享一组完整的值-您不保存任何冗余代码,因为它只使用一次。但是,如果网格区域存在,出于同样的原因,它同样是无用的。没有完全一样的东西,但是有一个功能可以类似地使用。是“将一组属性值应用于多个元素的方便方法”。如果有许多元素都需要将相同的属性设置为相同的值,则可以定义样式,如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style x:Key="AStyle" TargetType="TextBlock">
<Setter Property="Grid.Row" Value="1"/>
<Setter Property="Grid.Column" Value="1"/>
</Style>
</Grid.Resources>
<TextBlock Style="{StaticResource AStyle}" Text="Header" />
</Grid>
在您的示例代码中,这将是无用的,因为没有一个TextBlock
s共享一组完整的值-您不保存任何冗余代码,因为它只使用一次。但是网格区域
,如果它存在的话,出于同样的原因,也同样是无用的。WPF不直接支持这一点,但它很容易用依赖属性和附加属性实现。首先,您需要为AreaDefinition创建一个类:
public class AreaDefinition : DependencyObject
{
public int Row
{
get { return (int)GetValue(RowProperty); }
set { SetValue(RowProperty, value); }
}
public static readonly DependencyProperty RowProperty =
DependencyProperty.Register("Row", typeof(int), typeof(AreaDefinition), new PropertyMetadata(0));
public int Column
{
get { return (int)GetValue(ColumnProperty); }
set { SetValue(ColumnProperty, value); }
}
public static readonly DependencyProperty ColumnProperty =
DependencyProperty.Register("Column", typeof(int), typeof(AreaDefinition), new PropertyMetadata(0));
public int RowSpan
{
get { return (int)GetValue(RowSpanProperty); }
set { SetValue(RowSpanProperty, value); }
}
public static readonly DependencyProperty RowSpanProperty =
DependencyProperty.Register("RowSpan", typeof(int), typeof(AreaDefinition), new PropertyMetadata(1));
public int ColumnSpan
{
get { return (int)GetValue(ColumnSpanProperty); }
set { SetValue(ColumnSpanProperty, value); }
}
public static readonly DependencyProperty ColumnSpanProperty =
DependencyProperty.Register("ColumnSpan", typeof(int), typeof(AreaDefinition), new PropertyMetadata(1));
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(AreaDefinition), new PropertyMetadata(String.Empty));
}
您还需要一个类来保存这些内容的集合,其方式与Grid.Columns是ColumnDefinitionCollection类型的集合大致相同:
public class AreaDefinitionCollection : Collection<AreaDefinition>
{
}
有了它,您就可以实现您所追求的功能,只需稍作修改:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<g:GridHelper.AreaDefinitions>
<g:AreaDefinition Row="0" Column="0" ColumnSpan="2" Name="Header" />
<g:AreaDefinition Row="1" Column="0" Name="Navigation" />
<g:AreaDefinition Row="1" Column="1" Name="Main" />
<g:AreaDefinition Row="2" Column="0" ColumnSpan="2" Name="Footer" />
</g:GridHelper.AreaDefinitions>
<TextBlock g:GridHelper.Area="Header" Text="Header" />
<TextBlock g:GridHelper.Area="Navigation" Text="Navigation" />
<TextBlock g:GridHelper.Area="Main" Text="Main" />
<TextBlock g:GridHelper.Area="Footer" Text="Footer" />
</Grid>
WPF不直接支持这一点,但它很容易用依赖属性和附加属性实现。首先,您需要为AreaDefinition创建一个类:
public class AreaDefinition : DependencyObject
{
public int Row
{
get { return (int)GetValue(RowProperty); }
set { SetValue(RowProperty, value); }
}
public static readonly DependencyProperty RowProperty =
DependencyProperty.Register("Row", typeof(int), typeof(AreaDefinition), new PropertyMetadata(0));
public int Column
{
get { return (int)GetValue(ColumnProperty); }
set { SetValue(ColumnProperty, value); }
}
public static readonly DependencyProperty ColumnProperty =
DependencyProperty.Register("Column", typeof(int), typeof(AreaDefinition), new PropertyMetadata(0));
public int RowSpan
{
get { return (int)GetValue(RowSpanProperty); }
set { SetValue(RowSpanProperty, value); }
}
public static readonly DependencyProperty RowSpanProperty =
DependencyProperty.Register("RowSpan", typeof(int), typeof(AreaDefinition), new PropertyMetadata(1));
public int ColumnSpan
{
get { return (int)GetValue(ColumnSpanProperty); }
set { SetValue(ColumnSpanProperty, value); }
}
public static readonly DependencyProperty ColumnSpanProperty =
DependencyProperty.Register("ColumnSpan", typeof(int), typeof(AreaDefinition), new PropertyMetadata(1));
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(AreaDefinition), new PropertyMetadata(String.Empty));
}
您还需要一个类来保存这些内容的集合,其方式与Grid.Columns是ColumnDefinitionCollection类型的集合大致相同:
public class AreaDefinitionCollection : Collection<AreaDefinition>
{
}
有了它,您就可以实现您所追求的功能,只需稍作修改:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<g:GridHelper.AreaDefinitions>
<g:AreaDefinition Row="0" Column="0" ColumnSpan="2" Name="Header" />
<g:AreaDefinition Row="1" Column="0" Name="Navigation" />
<g:AreaDefinition Row="1" Column="1" Name="Main" />
<g:AreaDefinition Row="2" Column="0" ColumnSpan="2" Name="Footer" />
</g:GridHelper.AreaDefinitions>
<TextBlock g:GridHelper.Area="Header" Text="Header" />
<TextBlock g:GridHelper.Area="Navigation" Text="Navigation" />
<TextBlock g:GridHelper.Area="Main" Text="Main" />
<TextBlock g:GridHelper.Area="Footer" Text="Footer" />
</Grid>
我仍然对WPFIt的强大功能感到惊讶。有时有点太过分了我仍然对WPFIt的强大功能感到惊讶。有时有点太过分了在重温这个话题的时候,我意识到这个答案在某种程度上是正确的。我试图解决的问题是:你有一个20行的网格。现在,您希望在顶部附近插入一个新行。现在你必须在插入内容后修改所有内容的行号。重新访问这个主题时,我意识到这个答案在某种程度上是正确的,“哦,这太明显了”。我试图解决的问题是:你有一个20行的网格。现在,您希望在顶部附近插入一个新行。现在,您必须在插入内容之后修改所有内容的行号。