1

我有一个数据模板

<DataTemplate x:Key="ConnectorItemFactory">
    <Button Style="{StaticResource TextOnlyActionTileButtonStyle}" Margin="0,0,5,0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                HorizontalContentAlignment="Stretch"
                VerticalContentAlignment="Stretch"
                Content="{Binding}"
                ContentTemplate="{StaticResource TileTemplate}"
                Command="{StaticResource NavigationCommand}"
                CommandParameter="{Binding}"
        Height="156"
        Width="156"
        >
      <i:Interaction.Behaviors>
        <UICommon:XboxBehavior VuiBinding="{Binding VuiTitle}"/>
      </i:Interaction.Behaviors>
    </Button>
  </DataTemplate>

作为 ContentTemplate 它使用另一个数据模板

<DataTemplate x:Key="TileTemplate">
  <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{StaticResource ActionButtonBackgroundBrush}">
    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="7,7,7,4">
      <Controls:TrimmedTextBlock Text="{Binding Title}" Style="{StaticResource TextOnlyTileTitleStyle}" TextWrapping="Wrap" />
      <Controls:TrimmedTextBlock Text="{Binding Converter={StaticResource SubtitleTextFormatter}}" Style="{StaticResource TextOnlyTileSubtitleStyle}"/>
    </StackPanel>
  </Grid>
</DataTemplate>

我正在使用 TemplateFactory 加载内容模板

 public class GridTemplateFactory : ModelViewTemplateFactory
        {
            protected override void OnContentChanged(object oldContent, object newContent)
            {
                base.OnContentChanged(oldContent, newContent);

                var dataTemplate = Application.Current.Resources["ConnectorItemFactory"] as DataTemplate;

                // var grid = ((Button)dataTemplate.LoadContent()).ContentTemplate.LoadContent();
                // ((Grid)grid).Background = new SolidColorBrush(Colors.Orange);

                // ((Button)dataTemplate.LoadContent()).ContentTemplate.LoadContent().SetValue(Grid.BackgroundProperty, new SolidColorBrush(Colors.Orange));


                this.ContentTemplate = dataTemplate;
            }
        }

在 OnContentChanged 方法中,我想从 TileTemplate 数据模板更改网格背景颜色。我试着像上面注释的代码那样做,但它不起作用。我怎样才能在这里改变颜色?

4

0 回答 0