我在资源字典中定义了一个如下所示的资源:
<x:Int32 x:Key="HubSectionHeaderCharacterSpacing">-10</x:Int32>
<x:Double x:Key="HubSectionHeaderFontSize">19</x:Double>
<Thickness x:Key="HubSectionHeaderMarginThickness">-1,5,0,31.5</Thickness>
<Thickness x:Key="HubSectionMarginThickness">19,0,0,0</Thickness>
<Style x:Key="MainMenuHubSectionStyle2" TargetType="HubSection">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HubSection">
<Grid Background="WhiteSmoke">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.RenderTransform>
<CompositeTransform x:Name="WrappingTransform"/>
</Grid.RenderTransform>
<Viewbox HorizontalAlignment="Center" Margin="0">
<Image Source="ms-appx:///Assets/Logos/Logo.png" Stretch="Fill"/>
</Viewbox>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一个使用这种风格的HubSection。如何以编程方式访问此资源并替换
<Viewbox HorizontalAlignment="Center" Margin="0">
<Image Source="ms-appx:///Assets/Logos/Logo.png" Stretch="Fill"/>
</Viewbox>
还有其他一些形象吗?
答案 0 :(得分:0)
这是一个肮脏的黑客,但由于您只有一个要替换的属性,您可以利用Tag
属性来存储您的图片网址:
<Style x:Key="MainMenuHubSectionStyle2" TargetType="HubSection">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HubSection">
<Grid Background="WhiteSmoke">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.RenderTransform>
<CompositeTransform x:Name="WrappingTransform"/>
</Grid.RenderTransform>
<Viewbox HorizontalAlignment="Center" Margin="0">
<Image Source="{TemplateBinding Tag}" Stretch="Fill"/>
</Viewbox>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后你可以像这样使用它:
<HubSection Tag="ms-appx:///Assets/Logos/Logo.png">
</HubSection>
否则,您可能必须创建自己的类型,继承HubSection并添加必要的属性。
或者,您可以使用好的&#39;数据绑定,如RenDishen所建议的那样。例如:
<Style x:Key="MainMenuHubSectionStyle2" TargetType="HubSection">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="HubSection">
<Grid Background="WhiteSmoke">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.RenderTransform>
<CompositeTransform x:Name="WrappingTransform"/>
</Grid.RenderTransform>
<Viewbox HorizontalAlignment="Center" Margin="0">
<Image Source="{Binding Path=Logo}" Stretch="Fill"/>
</Viewbox>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="0" Grid.Row="1" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后,您必须为HubSection的DataContext
属性分配一个具有Logo
属性的对象,并相应地填充它。