【发布时间】:2016-01-29 18:07:56
【问题描述】:
我用我的自定义用户控件创建了一个 wpf 用户控件库,以便在不同的项目中使用它们。
我有一个带有默认图像的按钮,但我希望能够在需要时更改按钮的图像:
public static readonly DependencyProperty BTN_SEARCH_IMAGE =
DependencyProperty.Register("ButtonImage", typeof (string), typeof (Searchbar));
我的属性包装器:
public string ButtonImage
{
get
{
var strToCheck = GetValue(BTN_SEARCH_IMAGE) as string;
return String.IsNullOrWhiteSpace(strToCheck)
? @"WPF_Controls;Component/Images/Search.png"
: strToCheck;
}
set { SetValue(BTN_SEARCH_IMAGE, value); }
}
在 XAML 中:
<UserControl x:Class="WPF_Controls.Searchbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="SearchBar">
<DockPanel DataContext="{Binding ElementName=SearchBar}">
<Button Name="btnSearch"
DockPanel.Dock="Left"
Command="{Binding Path=SearchCommand}">
<Image Source="{Binding Path=ButtonImage}" />
</Button>
</DockPanel>
</UserControl>
如果用户没有将任何内容绑定到 ButtonImage,则应显示默认图像,否则用户可以选择。
【问题讨论】:
标签: c# wpf user-controls