在Silverlight中以编程方式设置网格背景颜色

时间:2013-03-25 14:33:32

标签: silverlight xaml silverlight-3.0

我有一个数据模板

<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数据模板中更改网格背景颜色。 我尝试像上面的注释代码那样做,但它不起作用。我怎样才能改变颜色?

0 个答案:

没有答案