有错误,但在运行时一切正常。
现在,我正在Cannot locate resource 'img/icons/silk/arrow_refresh.png'.
我有一个名为ImageButton的简单UserControl(不是每个人都有?):
<UserControl x:Class="MyProj.src.controls.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Button Name="btnButton" Click="btnButton_Click">
<StackPanel Orientation="Horizontal">
<Image Name="btnImage" Stretch="None" />
<TextBlock Name="btnText" />
</StackPanel>
</Button>
</UserControl>
您的期望是什么:
[ContentProperty("Text")]
public partial class ImageButton : UserControl
{
public String Image { set { btnImage.Source = GuiUtil.CreateBitmapImage(value); } }
public String Text { set { btnText.Text = value; } }
public double Gap { set { btnImage.Margin = new Thickness(0, 0, value, 0); } }
public bool ToolBarStyle { set { if (value) {
btnButton.Style = (Style)FindResource(ToolBar.ButtonStyleKey); } }
}
public bool IsCancel { set { btnButton.IsCancel = value; } }
public bool IsDefault { set { btnButton.IsDefault = value; } }
public event RoutedEventHandler Click;
public ImageButton()
{
InitializeComponent();
}
private void btnButton_Click(object sender, RoutedEventArgs e)
{
if (Click != null)
{
Click(sender, e);
}
}
}
CreateBitmapImage的位置如下:
public static BitmapImage CreateBitmapImage(string imagePath)
{
BitmapImage icon = new BitmapImage();
icon.BeginInit();
icon.UriSource = new Uri(String.Format("pack://application:,,,/{0}", imagePath));
icon.EndInit();
return icon;
}
我看不到任何使用像这样的ImageButton的xaml文件的设计视图:
<Window
foo="bar"
xmlns:wpfControl="clr-namespace:MyProj.src.controls">
<Grid>
<wpfControl:ImageButton ToolBarStyle="True" Gap="3"
Click="btnRefresh_Click" Text="Refresh"
Image="img/icons/silk/arrow_refresh.png" />
</Grid>
</Window>
为什么VS抱怨?
答案 0 :(得分:2)
我建议在创建Control时使用Dependency Properties和Bindings。这样做的好处很多,例如,您可以为ImageButton
绑定图像属性,并且不会遇到问题中的问题。
您可以将它们绑定到ImageButton
UserControl
的属性,而不是使用属性中的setter来设置Image源和TextBlock Text。
<强>更新强>
我在ImageButton
<UserControl ...
x:Name="imageButton">
<Button...>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=imageButton, Path=Image}" .../>
<TextBlock Text="{Binding ElementName=imageButton, Path=Text}" .../>
</StackPanel>
</Button>
</UserControl>
你的Xaml看起来像这样
public partial class ImageButton : UserControl
{
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("Image",
typeof(ImageSource),
typeof(ImageButton),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public ImageSource Image
{
get { return (ImageSource)base.GetValue(ImageProperty); }
set { base.SetValue(ImageProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string),
typeof(ImageButton),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public string Text
{
get { return (string)base.GetValue(TextProperty); }
set { base.SetValue(TextProperty, value); }
}
//...
}
然后在
后面的代码中替换CLR Properties with Dependency属性,如下所示PropertyChangedCallback
对于Gap属性,您可以使用转换器绑定边距,也可以使用{{1}}在代码后面设置它。
希望有所帮助