0

我不知道如何手动触发模板选择器(点击按钮)。在加载时使用 Template1,但想在 btn 按下 Tmaplte2 时进行更改。OnPorpertyChanged 有类似的东西吗?

我指的是 <ItemsControl x:Name="OptionItemsControl" ItemTemplateSelector="{StaticResource optionItemTemplateSelector}"-我想触发 ItemTemplateSelector 更改...

<!-- BODY (elements)-->
        <FlipView x:Name="OptionPagesFlipView"  Grid.Row="1" TabNavigation="Cycle" SelectionChanged="OptionPagesFlipView_SelectionChanged" ItemsSource="{Binding OptionsPageItems}">
            <FlipView.ItemTemplate>
                <DataTemplate x:Name="OptionMonthPageTemplate">
                    <ScrollViewer x:Name="OptionsScrollViewer" HorizontalScrollMode="Disabled" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto">
                        <Grid>
                            <ItemsControl x:Name="OptionItemsControl" ItemTemplateSelector="{StaticResource optionItemTemplateSelector}" ItemsSource="{Binding OptionItems, Mode=OneWay}" Visibility="{Binding OptionsPageVisibility}">                                
                            </ItemsControl>
                            <Grid x:Name="LoadingGrid" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding LoadingGridVisibility}">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <ProgressRing x:Name="CustomProgressRing" Height="40" Width="40" IsActive="true" Grid.Column="0" Margin="20" Foreground="White"/>
                                <TextBlock Text="Loading Data" x:Name="CustomTextBlock" Height="auto" Width="auto" FontSize="25" Grid.Column="1" Margin="20"/>
                            </Grid>
                        </Grid>
                    </ScrollViewer>
                </DataTemplate>
            </FlipView.ItemTemplate>

和资源:

<UserControl.Resources>
    <DataTemplate x:Key="template1">
        <Grid x:Name="OptionItemGrid" Background="White" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <!-- Content -->    
        </Grid>
    </DataTemplate>
    <DataTemplate x:Name="template2">
        <Grid x:Name="OptionItemGrid" Background="White" HorizontalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <!-- Content -->
        </Grid>
    </DataTemplate>

    <local:OptionDataItemTemplateSelector x:Key="optionItemTemplateSelector"
      Template1="{StaticResource template1}"
      Template2="{StaticResource template2}"/>
</UserControl.Resources>

和模板选择器类:

public class OptionDataItemTemplateSelector : DataTemplateSelector
{

    public DataTemplate Template1 { get; set; }
    public DataTemplate Template2 { get; set; }

    protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
    {
        if(someCondition == 1)
            return Template1;
        else
            return Template2;         
    }
}
4

2 回答 2

1

ItemTemplateSelectorItemsSource除非 中的orItem's实例ItemsSource未更改,否则无法触发。所以你想要达到ItemTemplateSelector的目的有点扭曲。执行此操作的简单方法是拥有您ViewModel/CodeBehind所说的财产

bool IsClicked
{
   get{return _isClicked;}
   set
   { 
     _isClicked = value
     RaisePropertyChanged("IsClicked");
   }
}

并在CommandHandler按钮或Click事件处理程序中切换此属性。

现在您可以为您的项目定义一个模板,如下所示,并ContentTemplate根据上面的属性切换它:

  <DataTemplate x:Key="myTemplate">
     <ContentControl Content="{Binding}">
       <ContentControl.Style>
           <Style TargetType="ContentControl">
             <Setter Property="ContentTemplate" Value="{StaticResource template1}"/>
              <Style.Triggers>
                  <DataTrigger Binding="{Binding DataContext.IsClicked, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="false">
                       <Setter Property="ContentTemplate" Value="{StaticResource template2}"/>
                   </DataTrigger
              </Style.Triggers>
           </Style>
       </ContentControl.Style>
     </ContentControl>
  </DataTemplate>
于 2013-10-22T14:36:27.833 回答
0

如何遍历您的项目并将 someCondition 设置为不为 1?

protected override Windows.UI.Xaml.DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
    if(ITEM.someCondition == 1)
        return Template1;
    else
        return Template2;         
}

在 SelectTemplate 方法中,您拥有所需的所有信息。该项目来自数据源和容器,该容器是您的数据源项目的当前视觉对象。使用这些信息来决定使用哪个模板。

更改条件,将返回另一个模板。

于 2013-10-22T14:11:44.200 回答