设置控件模板中的属性
本文关键字:属性 控件 设置 | 更新日期: 2023-09-27 18:02:24
我想在我的Image控件上分配一个URI。下面的XAML在UserControl中。
<StackPanel>
<Button Command="{StaticResource ImageClick}" x:Name="imageButton">
<Button.Template>
<ControlTemplate>
<Grid>
<Image MaxHeight="100" MaxWidth="300" x:Name="image" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
我在代码后面有一个依赖属性与图像的URL:
public string ImageUrl
{
get { return (string)GetValue(ImageUrlProperty); }
set { SetValue(ImageUrlProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageUrl. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageUrlProperty =
DependencyProperty.Register("ImageUrl", typeof(string), typeof(ImageControl), new PropertyMetadata(""));
什么是最好的做法来分配我的图像的源属性是在一个控制模板?
你可以使用一个内容展示器,然后仅仅把你的图像放在按钮的内容中。
<StackPanel>
<Button Command="{StaticResource ImageClick}"
x:Name="imageButton">
<Button.Template>
<ControlTemplate>
<Grid>
<ContentPresenter />
</Grid>
</ControlTemplate>
</Button.Template>
<Image MaxHeight="100"
MaxWidth="300"
x:Name="image" />
</Button>
</StackPanel>
在您的代码中使用PropertyChangedCallback来设置图像的源