【问题标题】:Setting columnDefinition and RowDefinition from style template?从样式模板设置 columnDefinition 和 RowDefinition?
【发布时间】:2018-07-21 20:51:48
【问题描述】:

是否可以添加以下代码:

<Grid.RowDefinitions>
    <RowDefinition Height="auto"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="auto"/>
</Grid.RowDefinitions>

Style / Setter 模板为Grid?然后像这样自动设置RowDefinitionsGrid

<Grid style="{StaticResource MyRowDefs}">
    <Button  Grid.Row="0" Content="Button1/>
    <TextBox Grid.Row="1"/>
    <Button  Grid.Row="2" Content="Button2/>
</Grid>

【问题讨论】:

    标签: wpf xaml templates uwp


    【解决方案1】:

    这样做可以解决您的问题:How to create reusable WPF grid layout

    对于您的示例,您可以执行以下操作:

    <Window x:Class="ReUseGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ReUseGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <Style x:Key="GridItemsStyle" TargetType="ItemsControl">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                            </Grid>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
        <ItemsControl Style="{StaticResource GridItemsStyle}">
            <Button Grid.Row="0" Content="Button1"/>
            <TextBox Grid.Row="1" Text="Test"/>
            <Button  Grid.Row="2" Content="Button2"/>
        </ItemsControl>
    </Window>
    

    【讨论】:

      【解决方案2】:

      您不能在Style 中设置RowDefinitions 属性,因为它不是依赖属性,但您可以创建自定义Grid 类型并使用该类型:

      public class CustomGrid : Grid
      {
          public CustomGrid()
          {
              RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
              RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) };
              RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
          }
      }
      

      用法:

      <local:CustomGrid>
          <Button  Grid.Row="0" Content="Button1/>
          <TextBox Grid.Row="1"/>
          <Button  Grid.Row="2" Content="Button2/>
      </local:CustomGrid>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多