8

私はWPFとMVVMパターンを学び、カレンダーのようなビューを構築しようとしています。したがって、現在、6行7列のグリッドがあります。最初の行はヘッダーである必要があります。したがって、「月曜日、火曜日など」などの曜日を指定します。現在、MonthView.xamlに次の情報があります。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
        <RowDefinition Height="{Binding RowHeight}"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
        <ColumnDefinition Width="{Binding CellWidth}"/>
    </Grid.ColumnDefinitions>

    <!-- Header Row-->
    <ContentPresenter Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DaysOfWeek[0]}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" DataContext="{Binding DaysOfWeek[1]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="2" DataContext="{Binding DaysOfWeek[2]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="3" DataContext="{Binding DaysOfWeek[3]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="4" DataContext="{Binding DaysOfWeek[4]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="5" DataContext="{Binding DaysOfWeek[5]}"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="6" DataContext="{Binding DaysOfWeek[6]}"/>

    <!-- 1st Row-->
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="0"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="1"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="2"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="3"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="4"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="5"/>
    <ContentPresenter ContentTemplate="{StaticResource CalendarCellTemplate}" Grid.Row="1" Grid.Column="6"/>

など:あなたは私が推測するパターンを見るでしょう。

これがCalendarHeaderCellTemplateです

<DataTemplate x:Key="CalendarHeaderCellTemplate">
    <StackPanel Margin="5" Background="Blue">
        <TextBlock Text="{Binding}"></TextBlock>
    </StackPanel>
</DataTemplate>

そして、これがViewModelの重要な部分です。

 public ObservableCollection<string> DaysOfWeek { get; private set; }
 public MonthViewModel()
    {  
        this.DaysOfWeek = new ObservableCollection<string> {DayOfWeek.Monday.ToString(), DayOfWeek.Tuesday.ToString(), DayOfWeek.Wednesday.ToString(), 
            DayOfWeek.Thursday.ToString(), DayOfWeek.Friday.ToString(), DayOfWeek.Saturday.ToString(), DayOfWeek.Sunday.ToString()};

    }

これで、DataTemplate'inline'を定義するContentpresenterは、TextBlock内にもCalendarHeaderCellTemplate内にも何も表示しません。

面白いことに、Visual Studioデザイナーの内部では、最初のセル(つまり、インラインテンプレートを持つセル)を除いて、すべてが正しく表示されます。

誰か提案がありますか。

注意:「インライン」テンプレートは、主にテスト目的で作成されました。

編集:ContentPresenterを使用する代わりにこれを行うと(以下を参照)、正常に機能します。たぶん私はContentPresenterを間違った方法で使用していますか?

 <StackPanel Grid.Row="0" Grid.Column="0">
     <TextBlock Text="{Binding DaysOfWeek[0]}"/>
 </StackPanel>

ContentPresenterを使用する理由は、すべてのセルのコンテンツのDataTemplateが、最終的には単なるテキストボックス以上のものになるためです。

4

2 に答える 2

24

ContentPresenterをに変更してみてください

    <ContentPresenter Content="{Binding DaysOfWeek[0]}" Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

また

  <ContentPresenter Content="{Binding}" Grid.Row="0" Grid.Column="0">
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DaysOfWeek[0]}"/>
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

また、DataContextを次のようなコンテンツに置き換えます

<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" Content="{Binding DaysOfWeek[1]}"/>
于 2010-12-24T08:39:36.633 に答える
5

ContentPresenterではなくContentControlを使用してみてください

<Style x:Key="CalendarCell" TargetType="ContentControl">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ContentControl">
        <StackPanel Margin="5" Background="{TemplateBinding Background}">
          <TextBlock Text="{TemplateBinding Content}" 
                     Foreground="{TemplateBinding Foreground}" />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<!-- Header Row-->
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[0]}" 
                Background="Blue" 
                Foreground="White" />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[1]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="1" />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[2]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="2"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[3]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="3"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[4]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="4"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[5]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="5"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="{Binding DaysOfWeek[6]}" 
                Background="Blue" 
                Foreground="White" 
                Grid.Column="6"  />

<!-- 1st Row-->
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="1" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="2" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="3" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="4" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="5" 
                Grid.Row="1"  />
<ContentControl Style="{StaticResource CalendarCell}" 
                Content="" 
                Background="Wheat" 
                Foreground="Black" 
                Grid.Column="6" 
                Grid.Row="1"  />
于 2010-12-24T08:57:30.137 に答える