在 WP7 上以编程方式更改 XAML 中的图像(列表框)

发布于 2024-12-10 03:20:29 字数 2327 浏览 0 评论 0原文

下面是我在本例中用于创建车辆列表的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2024-12-17 03:20:29

在模板中包含图像的两个版本,然后在每个版本上使用转换器,根据当前主题更改可见性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文