在 WP7 上以编程方式更改 XAML 中的图像(列表框)
下面是我在本例中用于创建车辆列表的 XAML——其中一辆车辆是“默认”车辆,如果默认设置设置为“true”,则旁边会显示一个星形图像。这一切都运行良好且花花公子。
但是,对于浅色/深色主题,我想根据主题将图像的颜色从白色更改为黑色。但是我不知道该怎么做。我尝试以编程方式创建 XAML 资源并添加它,但这似乎不起作用。我还尝试命名图像(正如您在“iLPNS”下面看到的那样,我无法在后面的代码中访问它,因为(我假设)它并不真正存在,直到列表框包含项目。
有人吗?我有什么想法可以做到这一点吗?
<controls:PivotItem Header="vehicles" Name="piLPNS">
<Grid>
<TextBlock Text="A list of the vehicles associated with your account are listed below. To make changes to your vehicle list using the buttons below or by tapping the desired vehicle." Margin="12,0,0,0" TextWrapping="Wrap" />
<ListBox Height="300" HorizontalAlignment="Left" Margin="0,135,0,0" Name="lbLPNList" VerticalAlignment="Top" Width="456" SelectionChanged="lbLPNList_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" Height="60" Width="432" HorizontalAlignment="Center">
<StackPanel Orientation="Vertical" Height="60" Width="40" Margin="12,0,0,0">
<StackPanel Orientation="Horizontal" Height="45">
<Image Name="iLPNS" Source="/Parkmobile;component/Resources/Icons/Icon.Star.Resource.png" Margin="0,5,0,0" Visibility="{Binding Default, Converter={StaticResource VisibilityConverter}}" Height="36" Width="36" HorizontalAlignment="Right"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical" Height="60">
<StackPanel Orientation="Horizontal" Height="45">
<TextBlock Margin="10,0,0,0" FontSize="36" Text="{Binding ShortDisplay}" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
Below is the XAML I'm using to create a list of vehicles in this instance -- one of the vehicles is the "default" vehicle and a star image is show next to it if the default setting is set to "true". This all works fine and dandy.
However, for light/dark themes, I'd like to change the color of the image from white to black depending on the theme. However I can't figure out how to do this. I've tried programatically creating an XAML resource and adding it but that doesn't seem to work. I've also tried naming the image (as you can see below "iLPNS" however, I can't access this in my code behind because (I'm assuming) it doesn't really exist until the listbox contains items.
Does anyone have any ideas how I can do this?
<controls:PivotItem Header="vehicles" Name="piLPNS">
<Grid>
<TextBlock Text="A list of the vehicles associated with your account are listed below. To make changes to your vehicle list using the buttons below or by tapping the desired vehicle." Margin="12,0,0,0" TextWrapping="Wrap" />
<ListBox Height="300" HorizontalAlignment="Left" Margin="0,135,0,0" Name="lbLPNList" VerticalAlignment="Top" Width="456" SelectionChanged="lbLPNList_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal" Height="60" Width="432" HorizontalAlignment="Center">
<StackPanel Orientation="Vertical" Height="60" Width="40" Margin="12,0,0,0">
<StackPanel Orientation="Horizontal" Height="45">
<Image Name="iLPNS" Source="/Parkmobile;component/Resources/Icons/Icon.Star.Resource.png" Margin="0,5,0,0" Visibility="{Binding Default, Converter={StaticResource VisibilityConverter}}" Height="36" Width="36" HorizontalAlignment="Right"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical" Height="60">
<StackPanel Orientation="Horizontal" Height="45">
<TextBlock Margin="10,0,0,0" FontSize="36" Text="{Binding ShortDisplay}" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模板中包含图像的两个版本,然后在每个版本上使用转换器,根据当前主题更改可见性。
Include both versions of the image in the template and then use a converter on each which changes the visibility based on the current theme.