将 ControlTemplate XAML 转换为 C#
我一直在试图将以下代码转换为纯 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>
到目前为止,我有以下内容,但出现错误。
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");
据我所知,我无法将 VisualBrush 作为 FrameworkElementFactory (崩溃),但是如果我将其声明为常规元素 VisualBrush,我就无法将边框作为 Visual 传递,因为它是 FrameworkElementFactory。
只是我迷路了,任何帮助将不胜感激。 感谢您的帮助
I have been stumped with trying to convert the following code into pure c#. This XAML code is from Cavanaghs blog on how to make rounded corners on anything. The code works but I need to convert it to c# as i need it to be dynamic in some cases. 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.
Simply i am getting lost, any help would be appreciated.
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您不必将其转换为 C# 来动态应用它。如果将其添加到您的应用程序资源中,请在 App.xaml 文件中如下所示:
请注意为该项目设置键的 x:Key 属性。
然后您可以在代码中的任何位置查找它...
然后您可以在需要时应用它!
You don't actually have to convert this into C# to apply it dynamically. If you add it to your application resources, within your App.xaml file as follows:
Note the x:Key attribute which keys this item.
You can then look it up anywhere in your code ...
You can then apply it as and when you need it!
你不想知道这一点。说真的,你不知道,这是一场噩梦。
编辑:如果我没有犯任何错误,这是您的代码的翻译...
编辑:仍然存在一个错误,
VisualBrush
不能这样创建,其余的似乎可以工作。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...
Edit: There is still a bug left, the
VisualBrush
cannot be created like that, the rest seems to work.