![](/img/trans.png)
[英]How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the GridView
[英]How to access a control placed inside data template of listbox?
嗨,我有以下代碼:
<ListBox x:Name="foldersListBox" Grid.Column="0" MouseLeftButtonUp="foldersListBox_MouseLeftButtonUp"
BorderThickness="0" Height="AUTO"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<DataTemplate>
<Border BorderBrush="LightGray"
BorderThickness="2"
CornerRadius="4">
<Image x:Name="folderImage" Width="70" Height="70" Margin="3" />
</Border>
</DataTemplate>
</ListBox>
現在,當我試圖從后面的代碼訪問folderImage
。 我可以使用加載的事件並將發送者作為圖像類型進行類型轉換,但我不希望這樣,因為我想在運行時綁定期間綁定圖像源。 因此,即使我們將嘗試加載事件,因為控件不會被加載,因此無法提供幫助。
幫助PLZ。
謝謝,Subhen
你的問題中有很多細節遺漏,但無論如何我都會勉強回答。 它與回答您的問題非常不同,但它可能會幫助您了解添加問題以指導答案所需的詳細信息。 反過來,這個答案可以改進。 在一些迭代中你可能實際上得到了答案。
我猜你是綁定到一組代表“文件夾”的對象,但你想要以編程方式修改所呈現的圖像,具體取決於每個對象的狀態,例如一些FolderType屬性。
解決方法是使用值轉換器,如果您的圖像來自有限集。
public class FolderToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Folder folder = value as Folder;
ImageSource result;
// Logic to determine which ImageSource to use for a folder.
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
現在來看看這個XAML: -
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<local:FolderToImageConverter x:Key="ImageConverter" />
</Grid.Resources>
<ListBox x:Name="foldersListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="4">
<Image Source="{Binding Converter={StaticResource ImageConverter}}" Width="70" Height="70" Margin="3" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
將Folder對象集合綁定到ListBox
ItemsSource
,它將使用轉換器顯示一組圖像,以將Folder
對象轉換為正確的ImageSource
實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.