如何以编程方式更改 Setter 值 Windows Phone 中的图像
How to change an Image in a Setter Value in Windows Phone programatically
我在资源字典中定义了一个如下所示的资源:
<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>
用其他图片?
这是一个肮脏的 hack,但由于您只有一个 属性 可以替换,您可以利用 Tag
属性 来存储您的图像 url:
<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 继承并添加必要的属性。
或者,您可以按照 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>
然后您必须将一个具有 Logo
属性 的对象分配给 HubSection 的 DataContext
属性,并相应地填充它。
我在资源字典中定义了一个如下所示的资源:
<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>
用其他图片?
这是一个肮脏的 hack,但由于您只有一个 属性 可以替换,您可以利用 Tag
属性 来存储您的图像 url:
<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 继承并添加必要的属性。
或者,您可以按照 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>
然后您必须将一个具有 Logo
属性 的对象分配给 HubSection 的 DataContext
属性,并相应地填充它。