我创建了一个usercontrol组合框,用于加载带描述的图像。因为我正在重复使用不同尺寸的图像,所以我想使用矢量图像。我已经完成了。我可以加载矢量图像,调整大小等。
我还想要将这些图像作为数据源的一部分填充组合框。我可以将svg加载为动态资源,但是我对组合框中的每个项目都有相同的图像。这当然不是我想要的。下面是xaml的一部分,我用它来创建我在组合框ItemTemplate中使用的图像和描述。
<DataTemplate x:Key="cmbTemplate" x:Name="cmbTemplate">
<WrapPanel Margin="0 0 0 0" Background="Transparent">
<Image Width="25" Height="25" Stretch="Fill" Source="{Binding Logo}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0">
</Image>
<Viewbox Width="125" Height="50">
<ContentPresenter Name="cmbContentPresenter" ContentTemplate="{DynamicResource btcvector}" />
</Viewbox>
<Label Content="{Binding Name}" Margin="0,0,15,0" FontSize="20"/>
</WrapPanel></DataTemplate>
视图框包含一个包含svg图像的contentpresenter。 svg图像在应用程序资源部分的app.xaml文件中声明。理想情况下,我想填充一个包含列表项的对象,例如image(svg资源) - 描述。我不知道从哪里开始。
答案 0 :(得分:0)
首先,您需要为每个项目设置一个代表类,必须从DependencyObject继承才能使此技巧生效。然后将Image和Description属性作为DependencyProperties添加到该类。例如
public class Item : DependencyObject {
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(SVGWhateverType), typeof(Item),new PropertyMetadata(default(SVGWhateverType)));
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Item),new PropertyMetadata(default(string)));
public SVGWhateverType Image {
get { return (SVGWhateverType)this.GetValue(ImageProperty); }
set { this.SetValue(ImageProperty, value); }
}
public string Name {
get { return (string)this.GetValue(NameProperty); }
set { this.SetValue(NameProperty, value); }
}
}
然后,您创建一个这些对象的列表,并将它们分配给ItemsSource-Property,就像您之前可能做的那样。
现在您只需要两个步骤:
item.Image = FindResource("ResourceNameHere");
而且你已经完成了。如果你真的想动态绑定图像,例如为了在运行时通过切换资源字典来交换它们,整个事情变得更加难以实现。