【问题标题】:WPF: Organisation of ControlsWPF:控件的组织
【发布时间】:2014-08-19 10:51:05
【问题描述】:

我正在尝试建立一个包含许多不同控件的“简单”窗口。

我遇到的问题是我只完成了 10% 并且我的 XAML 编码已经占用了很多行。按照不重复代码的概念,我想知道是否有更有效的方法来保持我的代码干净、整洁和高效。

有没有类似于资源字典或类的解决方案?

提前致谢!

这是我的 XAML 代码:

<Window
        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" mc:Ignorable="d" x:Class="League_of_Legends_Alpha_1_0.MainWindow"
        Title="MainWindow" Height="446" Width="1366">
    <Grid Background="Black" ShowGridLines="False">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="73*"></ColumnDefinition>
            <ColumnDefinition Width="42*"/>
            <ColumnDefinition Width="166*"/>
            <ColumnDefinition Width="269*"></ColumnDefinition>
            <ColumnDefinition Width="270*"></ColumnDefinition>
            <ColumnDefinition Width="270*"></ColumnDefinition>
            <ColumnDefinition Width="269*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="34*" MaxHeight="34"/>
            <RowDefinition Height="32*" MaxHeight="32"/>
            <RowDefinition Height="32*" MaxHeight="32"/>
            <RowDefinition Height="20*"/>
            <RowDefinition Height="19"/>
            <RowDefinition Height="20*"/>
            <RowDefinition Height="19*"/>
            <RowDefinition Height="32*"/>
            <RowDefinition Height="34*" MaxHeight="34"/>
            <RowDefinition Height="142*"/>
            <RowDefinition Height="31*"/>
        </Grid.RowDefinitions>
        <Viewbox Stretch="Uniform" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3">
            <Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="3" Grid.Row="0">
            <Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="4" Grid.Row="0">
            <Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="5" Grid.Row="0">
            <Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="6" Grid.Row="0">
            <Label Content="Summoner Name - Champion Name - Summoner Level" Foreground="White"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="3" Grid.Row="3">
            <Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="4" Grid.Row="3">
            <Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="5" Grid.Row="3">
            <Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
        </Viewbox>
        <Viewbox Stretch="Uniform" Grid.Column="6" Grid.Row="3">
            <Label Content="Summoner Name - Champion Name - Summoner Level"></Label>
        </Viewbox>
        <Image HorizontalAlignment="Left" Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Margin="5,0,5,0" Height="64" VerticalAlignment="Top" Width="64" Source="Blank.png" />
        <Image HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" Height="32" VerticalAlignment="Top" Width="32" Source="Blank.png" Margin="5,0,0,0"/>
        <Image HorizontalAlignment="Left" Grid.Row="2" Grid.Column="1" Height="32" VerticalAlignment="Top" Width="32" Source="Blank.png" Margin="5,0,0,0"/>
        <Image Grid.Column="1" HorizontalAlignment="Left" Height="32" Margin="5,3,0,0" Grid.Row="3" Grid.RowSpan="2" VerticalAlignment="Top" Width="32" Source="Blank.png"/>








        <Viewbox Grid.Column="2" Grid.Row="1" Stretch="UniformToFill">
            <Label Content="Runes" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" Width="166" Height="31" FontWeight="Bold"/>
        </Viewbox>
        <Viewbox Grid.Column="2" Grid.Row="2" Stretch="Fill">
            <Label Content="Masteries: 21/0/9" Grid.Column="2" HorizontalAlignment="Left" Height="32" Grid.Row="2" VerticalAlignment="Top" Width="166" Foreground="White" HorizontalContentAlignment="Center" FontWeight="Bold"/>
        </Viewbox>
        <Label Content="Challenger IV (100LP)" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="3" VerticalAlignment="Top" Width="161" Foreground="White" FontSize="9" FontWeight="Bold"/>
        <Label Content="K/D/A" HorizontalAlignment="Left" Grid.Row="3" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="9" FontWeight="Bold"/>
        <Label Content="Label" HorizontalAlignment="Left" Margin="10,5,0,0" Grid.Row="4" VerticalAlignment="Top"/>
        <Label Content="Games: 10000" HorizontalAlignment="Left" Height="19" Grid.Row="4" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="8.5" FontWeight="Bold"/>
        <Label Content="Win Ratio:" HorizontalAlignment="Left" Height="20" Grid.Row="5" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="9" FontWeight="Bold"/>
        <Label Content="Last Season: Challenger IV (100LP)" Grid.Column="2" HorizontalAlignment="Left" Height="20" Grid.Row="5" VerticalAlignment="Top" Width="166" FontSize="8.5" Background="Black" Foreground="White" Margin="0,1,0,0" Grid.RowSpan="2" FontWeight="Bold"/>
        <Label Content="Promotion Series: 3W/1L" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="4" VerticalAlignment="Top" Width="166" Foreground="White" FontSize="8.5" Height="19" FontWeight="Bold"/>
        <Label Content="100.5%" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="5" VerticalAlignment="Top" Height="20" Width="42" Foreground="White" FontSize="9" FontWeight="Bold"/>
        <Label Content="Normal Wins:" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Width="73" Foreground="White" FontSize="8" FontWeight="Bold"/>
        <Label Content="10000" Grid.Column="1" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Height="19" Width="42" Foreground="White" FontSize="8" FontWeight="Bold"/>
        <Label Content="OP.GG" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="6" VerticalAlignment="Top" Width="168" Foreground="White" FontSize="8" Grid.ColumnSpan="2" FontWeight="Bold"/>
        <!--
        <Border Grid.Column="0" Grid.RowSpan="2" BorderBrush="Aqua" BorderThickness="5" CornerRadius="8"/>
        -->
    </Grid>

</Window>

编辑/回答:

我首先做的是创建一个 ResoureDictionary 并添加一个带有网格的数据模板。

    <DataTemplate x:Key="myDataTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="4*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
    </grid>

然后在我的主窗口中,我使用 ContentControl 将数据模板绑定到我的网格。

    <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="32"/>
        <RowDefinition/>
        <RowDefinition Height="32"/>
        <RowDefinition/>
        <RowDefinition Height="32"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="0" Grid.Row="1"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="1" Grid.Row="1"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="2" Grid.Row="1"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="3" Grid.Row="1"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="4" Grid.Row="1"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="0" Grid.Row="3"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="1" Grid.Row="3"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="2" Grid.Row="3"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="3" Grid.Row="3"></ContentControl>
    <ContentControl ContentTemplate="{StaticResource myDataTemplate}" Grid.Column="4" Grid.Row="3"></ContentControl>
</Grid>

同样对于像我这样不熟悉 WPF 的人,您还需要引用资源(因为我使用了资源字典)

    <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MainDataTemplateDictionary.xaml">
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【问题讨论】:

  • 请参阅 MSDN 上的Control Authoring Overview 页面。特别是,您可能对 UserControl 感兴趣,以封装 UI 的可重用部分。
  • 请发布您的 UI 图片。以便我们提出更好的方法
  • link 这是用户界面的截图;最初,由于声誉低,我无法发布图片。
  • 顺便说一下,请注意区分“34”和“34*”(在您的实际代码中34* = 总宽度的8%)
  • 为了简化你的Grid(RowDefinitions和ColumnDefinition)的定义,我推荐使用SimpleGrid:thomaslevesque.fr/2010/07/20/…

标签: c# wpf xaml


【解决方案1】:

我认为你最好使用带有数据绑定模板的ItemsControl...类似这样的东西。

<ItemsControl ItemsSource="{Binding MyCollection}" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
           <Viewbox Stretch="Uniform">
            <!-- note that this requires a bindable property "SummonerDescription" in your viewmodel that combines the summoner name, champion name and summoner level -->
            <Label Content="{Binding SummonerDescription}" Foreground="White"></Label>
        </Viewbox>

        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

有关项目控件和数据绑定的更多信息,请查看here

在这种情况下,您可能希望将 ItemsControl 与在表中呈现单个“行”(或列?)的 UserControl 结合起来。

【讨论】:

  • 您如何将它们放入提供的网格中?看起来网格并不完全一致。
  • 应该提到我对 XAML 比较陌生,没有意识到数据绑定有多么强大。理解这个概念有点令人困惑,但我通过研究慢慢理解了这个概念;谢谢你的指点!尽管我可能需要更长的时间才能掌握这个概念并形成自信的理解。你能解释一下 ItemsControl 在这种情况下是如何起作用的吗?
  • @JasonLy ItemsControl 有点像StackPanel。虽然StackPanel 仅确定其子级的布局,但ItemsControl 可以提供对象集合以及确定如何呈现这些对象的模板。 ItemsControl 将根据提供的DataTemplate 呈现其集合中的每个项目。将其视为 ASP.NET 中的 Repeater
  • @JasonLy DataBinding 是一个强大的工具,但请谨慎使用,WPF 的“MVVM”编码风格会让事情很快变得混乱
  • ItemsControl 可能是一个不错的选择。请注意ItemsControl 的布局可以通过其ItemsPanel 进行修改。因此,可以将其修改为 Grid 或 Canvas,而不是默认的 StackPanel,以适合您的需要
【解决方案2】:

用于声明同一元素的重复属性值,例如

<Grid>
    <Viewbox Stretch="Uniform" x:Name="viewBox1">
    <Viewbox Stretch="Uniform" x:Name="viewBox2">
    <Viewbox Stretch="Uniform" x:Name="viewBox3">
</Grid>

您可以将“全局”定义为父(或前一级)容器中的样式,例如

<Grid>
    <Grid.Resources>
        <Style TargetType="ViewBox">
            <Setter Property="Stretch" Value="Uniform"/>
        </Style>
    </Grid.Resources>
    <Viewbox x:Name="viewBox1">
    <Viewbox x:Name="viewBox2">
    <Viewbox x:Name="viewBox3">
</Grid>

随后,您可以覆盖您想要的特定元素,例如

<Grid>
    <Grid.Resources>
        <Style TargetType="ViewBox">
            <Setter Property="Stretch" Value="Uniform"/>
        </Style>
    </Grid.Resources>
    <Viewbox x:Name="viewBox1">
    <Viewbox x:Name="viewBox2" Stretch="Fill">
    <Viewbox x:Name="viewBox3">
</Grid>

【讨论】:

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