我的代码是否展示了良好的WPF实践?

时间:2010-03-12 10:20:15

标签: c# wpf

我正在启动一个WPF项目,刚刚完成了UI的基础,但它似乎非常复杂,所以我不确定我是否已经以正确的方式进行了解决。我不想开始开发后端,并意识到我做错了前提,让自己的生活更加艰难。

从< DIV>和CSS的背景到风格,这有很多不同,并且真的想从一开始就把它弄好。

基本上它是一个星期的日历(7天,周一至周日,默认为当前一周。)最终将链接到数据库,如果我在这一天预约了它将在相关的时间显示它一天。

我选择了Grid而不是ListView,因为它的工作方式我不会将结果绑定到集合或其他任何行。相反,我将在每个活动中填写画布中的一个组合框(但是要放在代码中),并且在选择时它将向我显示更多细节。

XAML:

<Window x:Class="WOW_Widget.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:Extensions="clr-namespace:WOW_Widget"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="Window1" Height="239" Width="831">

    <Window.Resources>
        <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="1.0" Color="White"/>
                    <GradientStop Offset="0.0" Color="LightSlateGray"/>
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="grdDayHeader" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="Peru" />
                    <GradientStop Offset="1.0" Color="White" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="grdToday" StartPoint="0,0" EndPoint="0,1">
            <GradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0.0" Color="LimeGreen"/>
                    <GradientStop Offset="1.0" Color="DarkGreen" />
                </GradientStopCollection>
            </GradientBrush.GradientStops>
        </LinearGradientBrush>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <Setter Property="Background" Value="Khaki" />
        </Style>
        <Style x:Key="DayHeader" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="{StaticResource grdDayHeader}" />
            <Setter Property="Width" Value="111" />
            <Setter Property="Height" Value="25" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
        <Style x:Key="DayField">
            <Setter Property="Canvas.Width" Value="111" />
            <Setter Property="Canvas.Height" Value="60" />
            <Setter Property="Canvas.Background" Value="White" />
        </Style>
        <Style x:Key="Today">
            <Setter Property="Canvas.Background" Value="{StaticResource grdToday}" />
        </Style>
        <Style x:Key="CalendarColSpacer">
            <Setter Property="Canvas.Width" Value="1" />
            <Setter Property="Canvas.Background" Value="Black" />
        </Style>
        <Style x:Key="CalendarRowSpacer">
            <Setter Property="Canvas.Height" Value="1" />
            <Setter Property="Canvas.Background" Value="Black" />
        </Style>
    </Window.Resources>

    <Grid Background="{StaticResource NormalBrush}">
        <Border BorderBrush="Black" BorderThickness="1" Width="785" Height="86" Margin="12,12,12,104">
            <Canvas Height="86" Width="785" VerticalAlignment="Top">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Label Grid.Column="0" Grid.Row="0" Content="Monday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="1" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="2" Grid.Row="0" Content="Tuesday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="3" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="4" Grid.Row="0" Content="Wednesday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="5" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="6" Grid.Row="0" Content="Thursday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="7" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="8" Grid.Row="0" Content="Friday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="9" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="10" Grid.Row="0" Content="Saturday" Style="{StaticResource DayHeader}" />
                    <Canvas Grid.Column="11" Grid.RowSpan="3" Grid.Row="0"  Style="{StaticResource CalendarColSpacer}" />
                    <Label Grid.Column="12" Grid.Row="0" Content="Sunday" Style="{StaticResource DayHeader}" />

                    <Canvas Grid.Column="0" Grid.ColumnSpan="13" Grid.Row="1" Style="{StaticResource CalendarRowSpacer}" />

                    <Canvas Grid.Column="0" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblMondayDate" />
                    </Canvas>
                    <Canvas Grid.Column="2" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblTuesdayDate" />
                    </Canvas>
                    <Canvas Grid.Column="4" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblWednesdayDate" />
                    </Canvas>
                    <Canvas Grid.Column="6" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblThursdayDate" />
                    </Canvas>
                    <Canvas Grid.Column="8" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblFridayDate" />
                    </Canvas>
                    <Canvas Grid.Column="10" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblSaturdayDate" />
                    </Canvas>
                    <Canvas Grid.Column="12" Grid.Row="2" Margin="0" Style="{StaticResource DayField}">
                        <Label Name="lblSundayDate" />
                    </Canvas>
                </Grid>
            </Canvas>
        </Border>
        <Canvas Height="86" HorizontalAlignment="Right" Margin="0,0,12,12" Name="canvas1" VerticalAlignment="Bottom" Width="198"></Canvas>        
    </Grid>
</Window>

CS:

public partial class Window1 : Window {
    private DateTime today = new DateTime();
    private Label[] Dates = new Label[7];
    public Window1() {
        DateTime start = today = DateTime.Now;
        int day = (int)today.DayOfWeek;
        while (day != 1) {
            start = start.Subtract(new TimeSpan(1, 0, 0, 0));
            day--;
        }
        InitializeComponent();
        Dates[0] = lblMondayDate;
        Dates[1] = lblTuesdayDate;
        Dates[2] = lblWednesdayDate;
        Dates[3] = lblThursdayDate;
        Dates[4] = lblFridayDate;
        Dates[5] = lblSaturdayDate;
        Dates[6] = lblSundayDate;
        FillWeek(start);
    }

    private void FillWeek(DateTime start) {
        for (int d = 0; d &lt; Dates.Length; d++) {
            TimeSpan td = new TimeSpan(d, 0, 0, 0);
            DateTime _day = start.Add(td);
            if (_day.Date == today.Date) {
                Canvas dayCanvas = (Canvas)Dates[d].Parent;
                dayCanvas.Style = (Style)this.Resources["Today"];
            }
            Dates[d].Content = (int)start.Add(td).Day;
        }
    }
}

3 个答案:

答案 0 :(得分:18)

我会说,不,你没有以正确的方式解决这个问题。你正在做太多的工作,让WPF做得太少。你应该使用数据绑定和ItemsControl,让WPF尽一切可能搞清楚把东西放在哪里以及放入什么内容。

  1. 我在这个例子中使用了XmlDataProvider,因为这是演示如何在不编写代码的情况下使用数据绑定的最简单方法。您可能构建了一个视图模型类,它暴露了具有Name和Date属性的对象集合,并绑定到该实例。

  2. 在WPF中使用Canvas和逐像素布局是一件坏事,这与构建独立于分辨率的UI有关。让布局引擎完成所有工作要好得多。

  3. 并非所有看起来像网格的内容都需要使用Grid进行布局。如果将此网格作为水平StackPanel放置,则会有更多,更少的XAML。这使您不必使用行号和列号。我的布局使用Grid的唯一原因是将边框大小设为其内容。

  4. 这是我对你提供的内容的修改。在我看来,结果与你的结果非常相似,但它大约是XAML的一半(不计算画笔),并且更容易修改。

    <Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Background="{DynamicResource NormalBrush}">
    
      <Page.Resources>
           <XmlDataProvider x:Key="Days" XPath="Days">
            <x:XData>
              <Days xmlns="">
                <Day Name="Sunday" Date="03/14/2010"/>
                <Day Name="Monday" Date="03/15/2010"/>
                <Day Name="Tuesday" Date="03/16/2010"/>
                <Day Name="Wednesday" Date="03/17/2010"/>
                <Day Name="Thursday" Date="03/18/2010"/>
                <Day Name="Friday" Date="03/19/2010"/>
                <Day Name="Saturday" Date="03/20/2010"/>
              </Days>
            </x:XData>
           </XmlDataProvider>
    
            <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="1.0" Color="White"/>
                        <GradientStop Offset="0.0" Color="LightSlateGray"/>
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>
    
            <LinearGradientBrush x:Key="DayHeaderBrush" StartPoint="0,0" EndPoint="0,1">
                <GradientBrush.GradientStops>
                    <GradientStopCollection>
                        <GradientStop Offset="0.0" Color="Peru" />
                        <GradientStop Offset="1.0" Color="White" />
                    </GradientStopCollection>
                </GradientBrush.GradientStops>
            </LinearGradientBrush>        
    
      </Page.Resources>
            <Grid Margin="50">
              <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>            
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
              </Grid.ColumnDefinitions>
              <StackPanel Orientation="Horizontal">
                <ItemsControl ItemsSource="{Binding Source={StaticResource Days}, XPath=Day}">
                  <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                      <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                  </ItemsControl.ItemsPanel>
                  <ItemsControl.ItemTemplate>
                    <DataTemplate>
                    <StackPanel Orientation="Vertical" Background="White">
                        <Border BorderBrush="Black" BorderThickness="1,1,0,0" Background="{StaticResource DayHeaderBrush}">
                          <TextBlock Margin="30,10" HorizontalAlignment="Center" Text="{Binding XPath=@Name}"/>
                        </Border>
                        <Border BorderBrush="Black" BorderThickness="1,1,0,0">
                          <TextBlock Margin="30,10" Height="50" HorizontalAlignment="Center" Text="{Binding XPath=@Date}"/>
                        </Border>
                      </StackPanel>
                    </DataTemplate>
                  </ItemsControl.ItemTemplate>
                </ItemsControl>
                <Border BorderBrush="Black" BorderThickness="0,0,1,0"/>
              </StackPanel>
            </Grid>
    </Page>
    

答案 1 :(得分:5)

我不知道你的项目有多大,但是在启动WPF时我能给你的最大提示是研究PRISM框架:

http://www.codeplex.com/CompositeWPF

http://msdn.microsoft.com/en-us/magazine/cc785479.aspx

这是一个很好的框架,为您解决了很多问题。

关于您的日历,我建议将其设为可重复使用的无外观控件,本文可能会对您有所帮助:

http://www.codeproject.com/KB/WPF/WPFOutlookCalendar.aspx

答案 2 :(得分:4)

如果您以后决定使用绑定,则可以遵循MVVM模式。有关详细信息:

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

如果您想每天单独显示约会,可以使用“RelayCommand”转到第二天。