【发布时间】:2020-02-16 12:35:28
【问题描述】:
我在 XAML 中创建了一个图像,我想从代码中访问它以动态修改源。到目前为止我无法访问。有什么解决办法吗?
<Window x:Class="DDSV2.MasterMain"
Name="MainWw"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DDSV2"
mc:Ignorable="d"
Title="MasterMain" Height="1123.439" Width="1822.363" Loaded="Window_Loaded">
<Grid Name="MainGrid">
<ListView x:Name="MainMasterLv" Height="333" Margin="10,10,1494,0" VerticalAlignment="Top" SelectionChanged="MainMasterLv_SelectionChanged">
<ListView.View>
<GridView x:Name="GridVw">
<GridViewColumn x:Name="DataGridCn" Header="">
<GridViewColumn.CellTemplate>
<DataTemplate x:Name="DataTmp">
<StackPanel Orientation="Horizontal">
<Image x:Name="AddImg" Source="pack://application:,,,/Resources /gears.png" MaxWidth="80" MaxHeight="80" Margin="3,3,3,3"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
【问题讨论】:
-
@Clint 你不能这样做,因为图像在 DataTemplate 中。
-
为什么需要从后面的代码中访问 Image 元素?您可以将其任何属性绑定到 ListView 使用的项类的属性。特别是
<Image Source="{Binding YourProperty}"/>。当然,您也可以在一个或多个设置 Source 属性的属性上使用带有转换器的绑定,甚至是 DataTriggers -
@Clemens 感谢您的想法。最后,我使用了 Binding 并更改了 Property 值。谢谢!