我试图在我的用户控件中设置:
d:DataContext="{d:DesignData ListItemDemoData.xaml}"
但Visual Studio 声明它无法找到ListItemDemoData
.ListItemDemoData.xaml
ListItemDemoData
奇怪,我做错了吗?
我试图在我的用户控件中设置:
d:DataContext="{d:DesignData ListItemDemoData.xaml}"
但Visual Studio 声明它无法找到ListItemDemoData
.ListItemDemoData.xaml
ListItemDemoData
奇怪,我做错了吗?
你可以在后面的代码上做到这一点。我有一个用于显示大图像的用户控件。我的控件的名称是 ImageViewer。现在我必须绑定图像源。所以,我创建了一个依赖属性,比如
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("ImageSourceValue", typeof(string), typeof(ImageViewer),
new PropertyMetadata(ValueChanged));
public string ImageSourceValue
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
/// <summary>
/// Called when the value of ImageSourceValue is changed or set.
/// </summary>
public static void ValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
// Get out of a static and into the instance ASAP.
ImageViewer control = (ImageViewer)sender;
control.ValueChanged();
}
private void ValueChanged()
{
//The value of ImageSourceValue is set at the time calling user control from your page.
this.ImageSource = ImageSourceValue;
this.DataContext = this;
}
public string ImageSource { get; set; }
在将值绑定到将字符串图像路径转换为位图图像对象的图像控件时,我使用了转换器。string ImageSource 可以改为 BitmapImage 类型。并且当 ImageSourceValue 的值被赋值给 ImageSource 时就可以完成转换。