【问题标题】:PivotItem header style when Pivots ItemsSource is set to datacontext MVVMPivotItemsSource 设置为 datacontext MVVM 时的 PivotItem 标头样式
【发布时间】:2014-10-22 13:26:27
【问题描述】:

我正在应用 MVVM 模式,当我为数据创建 ViewModel 时,我将我的数据透视的 ItemsSource 设置如下:

<Pivot ItemsSource="{Binding DataContext}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1"/>

但我希望我的枢轴项目具有这样的标题

<PivotItem.Header>
    <TextBlock Margin="0, 16, 0, 0" Text="settings" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</PivotItem.Header>

在 xaml 或代码隐藏的什么位置设置它,如果是最后一个,究竟在 ViewModel 的哪个位置?

这就是我的 Pivot xaml 在 MainPage.xaml 中的样子:

<Pivot ItemsSource="{Binding DataContext}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">
        <Pivot.ItemTemplate>
            <DataTemplate>
                <PivotItem Margin="8,8,8,0">
                    <PivotItem.Header>
                        <TextBlock Margin="0, 16, 0, 0" Text="{Binding S}" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
                    </PivotItem.Header>
                </PivotItem>
            </DataTemplate>
        </Pivot.ItemTemplate>
    </Pivot>

在 MainPage.xaml.cs 我有

this.DataContext = new MyPageViewModel();

var viewModel = (MyPageViewModel)this.DataContext;

if (true == viewModel.LoadDataCommand.CanExecute(null))
{
   viewModel.LoadDataCommand.Execute(null);
}

但是什么都没有发生..

【问题讨论】:

    标签: c# wpf xaml mvvm binding


    【解决方案1】:

    V.G.,

    我回答了您的另一个问题,其中问题的根本原因是您的 XAML 将 PivotItem 包含在 DataTemplate 中的方式。简单地说,不要那样做。 :) 这来自您正在构建的同一个应用程序,因此它可能会解决您的问题。

    查看我对PivotItems do not resize after binding 的回答,并将您的 XAML 重构为我所描述的内容。如果您仍然遇到标题绑定问题,请给我发消息,我会回来帮助您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2010-11-24
      • 2023-03-25
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多