Nit*_*hos 3 c# wpf xaml frameworkelementfactory
我一直难以尝试将以下代码转换为纯c#.这个XAML代码来自Cavanaghs的博客,介绍如何在任何方面制作圆角.代码有效但我需要将其转换为c#,因为在某些情况下我需要它是动态的.如果你能提供帮助那就太好了.
<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>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有以下内容,但我收到错误.
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");
Run Code Online (Sandbox Code Playgroud)
据我所知,我不能将VisualBrush作为FrameworkElementFactory(崩溃),但如果我将其声明为常规元素VisualBrush,我不能将其作为VisualE传递边界,因为它是一个FrameworkElementFactory.
只是我迷路了,任何帮助将不胜感激.谢谢你的帮助
您实际上不必将其转换为C#以动态应用它.如果将其添加到应用程序资源中,请在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>
Run Code Online (Sandbox Code Playgroud)
请注意键入此项目的x:Key属性.
然后,您可以在代码中的任何位置查找...
ControlTemplate template = this.Findresource("MyListViewItemTemplate") as ControlTemplate
Run Code Online (Sandbox Code Playgroud)
然后,您可以在需要时应用它!