我正在拼命地尝试绑定一个映像(该映像位于应用程序安装目录的子目录中)。我使用MVVM和。net 3.5,是不容易找到图像路径和返回字符串,Uri或位图在属性和使用它在xaml.
我尝试了所有这三种格式绑定到UriSource中的图像。资源,但都没用。我也尝试绑定UriSource与一个字符串属性与转换器,没有运气!
不显示任何图像。
这有可能实现吗?那我错过了什么?
xaml:
<TreeView ItemsSource="{Binding ObCol_FamilyTree}">
<i:Interaction.Behaviors>
<behaviours:BindableTreeViewSelectedItemBehavior SelectedItem="{Binding TreeViewSelectedItem, Mode=TwoWay}" />
</i:Interaction.Behaviors>
<TreeView.Resources>
<Style x:Key="ExpandingImageStyle" TargetType="{x:Type Image}">
<Setter Property="Source" Value="{DynamicResource Icon_Closed}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
<Setter Property="Source" Value="{DynamicResource Icon_Open}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ObCol_Items}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="0,0,3,0" Style="{StaticResource ExpandingImageStyle}">
<Image.Resources>
<BitmapImage x:Key="Icon_Closed" UriSource="{Binding OpenFolderPath, Converter={StaticResource UriConverter}}"/>
<BitmapImage x:Key="Icon_Open" UriSource="{Binding OpenFolderPath, Converter={StaticResource UriConverter}}" />
</Image.Resources>
</Image>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Viewmodel:
private string myOpenFolderPath;
public ItemListVM()
{
myOpenFolderPath = AppDomain.CurrentDomain.BaseDirectory + @"..imagesIcon_Open.png";
if (!File.Exists(myOpenFolderPath))
myOpenFolderPath = String.Empty;
}
public string OpenFolderPath
{
get { return Path.GetFullPath(myOpenFolderPath); }
}
转换器:
public class UriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new Uri(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
我尝试了同样的Uri属性与一个正确的转换器,它返回一个Uri,没有更多的运气!
编辑:解决方案如下:
<TreeView ItemsSource="{Binding ObCol_FamilyTree}">
<i:Interaction.Behaviors>
<behaviours:BindableTreeViewSelectedItemBehavior SelectedItem="{Binding TreeViewSelectedItem, Mode=TwoWay}" />
</i:Interaction.Behaviors>
<TreeView.Resources>
<Style x:Key="ExpandingImageStyle" TargetType="{x:Type Image}">
<Setter Property="Source" Value="{Binding DataContext.ClosedFolderPath, RelativeSource={RelativeSource AncestorType=TreeView}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
<Setter Property="Source" Value="{Binding DataContext.OpenFolderPath, RelativeSource={RelativeSource AncestorType=TreeView}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding ObCol_Items}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="0,0,3,0" Style="{StaticResource ExpandingImageStyle}"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
BitmapImage资源和Binding Converter都不需要。
这是开箱即用的,因为内置了从string
到ImageSource
:
<Setter Property="Source" Value="{Binding OpenedFolderPath}"/>
在TreeView的ItemTemplate中,这个绑定将不起作用,因为它没有预期的DataContext。你会写
<Setter Property="Source"
Value="{Binding DataContext.OpenedFolderPath,
RelativeSource={RelativeSource AncestorType=TreeView}}"/>
除此之外,BitmapImage的UriSource
属性的转换器必须返回一个Uri,而不是另一个BitmapImage:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return new Uri(value.ToString());
}