我设计了C#代码来创建一个ImageButton(下图),它有三个图像(一个基本图像和两个叠加)和三个文本框作为按钮的面。我继承了Button类,遗憾的是它包含了几个我没想到的组件,直到编码后需要删除,即IsMouseOver上的亮蓝色环绕边框,以及按钮之间的任何可见边框,如按钮将以wrapPanel结束,边框需要无缝连接。现在格式已经在C#中完成,我希望我需要转换为XAML,这样我就可以创建一个ControlTemplate来获得必要的功能,但是我不确定从C#转换到XAML的过程。如果有人知道一个好的概述/资源,讨论转换所需的内容以便我能够正确翻译,我将不胜感激?感谢。
public class ACover : Button
{
Image cAImage = null;
Image jCImage = null;
Image jCImageOverlay = null;
TextBlock ATextBlock = null;
TextBlock AbTextBlock = null;
TextBlock RDTextBlock = null;
private string _TracksXML = "";
public ACover()
{
Grid cArtGrid = new Grid();
cArtGrid.Background = new SolidColorBrush(Color.FromRgb(38, 44, 64));
cArtGrid.Margin = new System.Windows.Thickness(5, 10, 5, 10);
RowDefinition row1 = new RowDefinition();
row1.Height = new GridLength(225);
RowDefinition row2 = new RowDefinition();
row2.Height = new GridLength(0, GridUnitType.Auto);
RowDefinition row3 = new RowDefinition();
row3.Height = new GridLength(0, GridUnitType.Auto);
RowDefinition row4 = new RowDefinition();
row4.Height = new GridLength(0, GridUnitType.Auto);
cArtGrid.RowDefinitions.Add(row1);
cArtGrid.RowDefinitions.Add(row2);
cArtGrid.RowDefinitions.Add(row3);
cArtGrid.RowDefinitions.Add(row4);
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = new GridLength(0, GridUnitType.Auto);
cArtGrid.ColumnDefinitions.Add(col1);
jCImage = new Image();
jCImage.Height = 240;
jCImage.Width = 260;
jCImage.VerticalAlignment = VerticalAlignment.Top;
jCImage.Source = new BitmapImage(new Uri(Properties.Settings.Default.pathToGridImages + "jc.png", UriKind.Absolute));
cArtGrid.Children.Add(jCImage);
cArtImage = new Image();
cArtImage.Height = 192;
cArtImage.Width = 192;
cArtImage.Margin = new System.Windows.Thickness(3, 7, 0, 0);
cArtImage.VerticalAlignment = VerticalAlignment.Top;
cArtGrid.Children.Add(cArtImage);
jCImageOverlay = new Image();
jCImageOverlay.Height = 192;
jCImageOverlay.Width = 192;
jCImageOverlay.Margin = new System.Windows.Thickness(3, 7, 0, 0);
jCImageOverlay.VerticalAlignment = VerticalAlignment.Top;
jCImageOverlay.Source = new BitmapImage(new Uri( Properties.Settings.Default.pathToGridImages + "jc-overlay.png", UriKind.Absolute));
cArtGrid.Children.Add(jCImageOverlay);
ATextBlock = new TextBlock();
ATextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
ATextBlock.Margin = new Thickness(10, -10, 0, 0);
cArtGrid.Children.Add(ATextBlock);
AlTextBlock = new TextBlock();
AlTextBlock.Margin = new Thickness(10, 0, 0, 0);
AlTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
cArtGrid.Children.Add(AlTextBlock);
RDTextBlock = new TextBlock();
RDTextBlock.Margin = new Thickness(10, 0, 0, 0);
RDTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(173, 176, 198));
cArtGrid.Children.Add(RDTextBlock);
Grid.SetColumn(jCImage, 0);
Grid.SetRow(jCImage, 0);
Grid.SetColumn(jCImageOverlay, 0);
Grid.SetRow(jCImageOverlay, 0);
Grid.SetColumn(cArtImage, 0);
Grid.SetRow(cArtImage, 0);
Grid.SetColumn(ATextBlock, 0);
Grid.SetRow(ATextBlock, 1);
Grid.SetColumn(AlTextBlock, 0);
Grid.SetRow(AlTextBlock, 2);
Grid.SetColumn(RDTextBlock, 0);
Grid.SetRow(RDTextBlock, 3);
this.Content = cArtGrid;
}
public string A
{
get { if (ATextBlock != null) return ATextBlock.Text;
else return String.Empty; }
set { if (ATextBlock != null) ATextBlock.Text = value; }
}
public string Al
{
get { if (AlTextBlock != null) return AlTextBlock.Text;
else return String.Empty; }
set { if (AlTextBlock != null) AlTextBlock.Text = value; }
}
public string RD
{
get { if (RDTextBlock != null) return RDTextBlock.Text;
else return String.Empty; }
set { if (RDTextBlock != null) RDTextBlock.Text = value; }
}
public ImageSource Image
{
get { if (cArtImage != null) return cArtImage.Source;
else return null; }
set { if (cArtImage != null) cArtImage.Source = value; }
}
public string TracksXML
{
get { return _TracksXML; }
set { _TracksXML = value; }
}
public double ImageWidth
{
get { if (cArtImage != null) return cArtImage.Width;
else return double.NaN; }
set { if (cArtImage != null) cArtImage.Width = value; }
}
public double ImageHeight
{
get { if (cArtImage != null) return cArtImage.Height;
else return double.NaN; }
set { if (cArtImage != null) cArtImage.Height = value; }
}
}
答案 0 :(得分:1)
XAML基本上代表一个对象图,因此翻译通常应该是非常机械的:
new
转换为XAML元素标记,例如Grid cArtGrid = new Grid();
转换为<Grid Name="cArtGrid"></Grid>
。cArtGrid.Background = new SolidColorBrush(Color.FromRgb(38, 44, 64));
转换为<Grid Background="#FF262C40">
。<Grid><Grid.RowDefinitions><RowDefinition Height="225" /></Grid.RowDefinitions></Grid>
。cArtGrid.Children.Add(jCImage);
变为<Grid><Image ... /></Grid>
。 (这同样适用于Content属性,虽然这不会对您产生任何影响。)Grid.SetColumn(ATextBlock, 0);
变为<Grid><TextBlock Grid.Column="0" /></Grid>
。通常,在MSDN中查找属性并查看XAML语法应该会给你一个良好的开端。
答案 1 :(得分:1)
听起来你正在尝试使用一些新功能扩展按钮。因此,您要做的第一件事就是利用依赖系统,以便您的属性可以绑定在XAML中。请查看此article on MSDN以获取有关声明新依赖项属性的信息。
依赖属性的主要候选者是Image属性。
实际上,我建议在Visual Studio中使用新的CustomControl模板为您提供一些样板代码。部分样板文件声明了一个themes.xaml文件,该文件为您的控件提供了一个默认模板。该模板将保留您翻译的XAML以供您控制。
XAML的好处在于它是一种初始化语言。获得在AlbumCover上声明的依赖项属性后,您将在控件的模板中绑定它们。有关其工作原理的更多详细信息,请参阅Charles Petzold关于creating lookless controls in WPF的文章。
您已经掌握了控件的基本外观和功能。遵循这两个资源应该可以帮助您集成到WPF生态系统中。