[英]Converting ControlTemplate XAML to C#
I have been stumped with trying to convert the following code into pure c#. 我一直难以尝试将以下代码转换为纯c#。 This XAML code is from Cavanaghs blog on how to make rounded corners on anything.
这个XAML代码来自Cavanaghs的博客,介绍如何在任何方面制作圆角。 The code works but I need to convert it to c# as i need it to be dynamic in some cases.
代码有效但我需要将其转换为c#,因为在某些情况下我需要它是动态的。 If you could help that would be great.
如果你能提供帮助那就太好了。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType='{x:Type ListViewItem}'>
<Grid>
<Border CornerRadius="15" Name="mask" Background="White"/>
<StackPanel Background="Beige">
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</StackPanel.OpacityMask>
<GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}"/>
<TextBlock Background="LightBlue" Text="{Binding News}" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
So far I have the following but I am getting errors. 到目前为止,我有以下内容,但我收到错误。
FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BackgroundProperty, Brushes.White);
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(8, 8, 8, 8));
border.SetValue(Border.NameProperty, "roundedMask");
As far as I can tell I cant make the VisualBrush as a FrameworkElementFactory (crashes), but if i declare it as a regular element VisualBrush i cant pass border in as a Visual since its a FrameworkElementFactory. 据我所知,我不能将VisualBrush作为FrameworkElementFactory(崩溃),但如果我将其声明为常规元素VisualBrush,我不能将其作为VisualE传递边界,因为它是一个FrameworkElementFactory。
Simply i am getting lost, any help would be appreciated. 只是我迷路了,任何帮助将不胜感激。 Thanks for any help
谢谢你的帮助
You don't actually have to convert this into C# to apply it dynamically. 您实际上不必将其转换为C#以动态应用它。 If you add it to your application resources, within your App.xaml file as follows:
如果将其添加到应用程序资源中,请在App.xaml文件中添加如下:
<Application.Resources>
<ControlTemplate TargetType='{x:Type ListViewItem}' x:Key="MyListViewItemTemplate">
<Grid>
<Border CornerRadius="15" Name="mask" Background="White"/>
<StackPanel Background="Beige">
<StackPanel.OpacityMask>
<VisualBrush Visual="{Binding ElementName=mask}"/>
</StackPanel.OpacityMask>
<GridViewRowPresenter Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}"/>
<TextBlock Background="LightBlue" Text="{Binding News}" />
</StackPanel>
</Grid>
</ControlTemplate>
</Application.Resources>
Note the x:Key attribute which keys this item. 请注意键入此项目的x:Key属性。
You can then look it up anywhere in your code ... 然后,您可以在代码中的任何位置查找...
ControlTemplate template = this.Findresource("MyListViewItemTemplate") as ControlTemplate
You can then apply it as and when you need it! 然后,您可以在需要时应用它!
You do not want to know this. 你不想知道这个。 Seriously, you don't, it's a nightmare.
说真的,你没有,这是一场噩梦。
Edit: If i did not make any mistake this is the translation of your code... 编辑:如果我没有犯任何错误,这是你的代码的翻译...
Setter setter = new Setter();
setter.Property = ListViewItem.TemplateProperty;
ControlTemplate template = new ControlTemplate(typeof(ListViewItem));
var grid = new FrameworkElementFactory(typeof(Grid));
var border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BackgroundProperty, Brushes.White);
border.SetValue(Border.NameProperty, "mask");
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(15));
grid.AppendChild(border);
var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.SetValue(StackPanel.BackgroundProperty, Brushes.Beige);
var visualBrush = new FrameworkElementFactory(typeof(VisualBrush));
visualBrush.SetBinding(VisualBrush.VisualProperty, new Binding() { ElementName = "mask" });
stackPanel.SetValue(StackPanel.OpacityMaskProperty, visualBrush);
var gridViewRowPresenter = new FrameworkElementFactory(typeof(GridViewRowPresenter));
gridViewRowPresenter.SetValue(GridViewRowPresenter.ContentProperty, new TemplateBindingExtension(GridViewRowPresenter.ContentProperty));
gridViewRowPresenter.SetValue(GridViewRowPresenter.ColumnsProperty, new TemplateBindingExtension(GridView.ColumnCollectionProperty));
stackPanel.AppendChild(gridViewRowPresenter);
var textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.BackgroundProperty, Brushes.LightBlue);
textBlock.SetBinding(TextBlock.TextProperty, new Binding("News"));
stackPanel.AppendChild(textBlock);
grid.AppendChild(stackPanel);
template.VisualTree = grid;
setter.Value = template;
Edit: There is still a bug left, the VisualBrush
cannot be created like that, the rest seems to work. 编辑:还有一个bug,
VisualBrush
无法像这样创建,其余的似乎工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.