本文介绍了在代码中转换此XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,我正在尝试创建一个CustomUser控件,因为我需要一个基本模板,用于我的应用程序中的所有userControls(我正在尝试创建一个单窗口应用程序),其中包含一种Window-in-Window ,其中userControls是MainWindow.xaml中的一种窗口。 为了实现它,我尝试使用以下模板在XAML和CS代码中创建我的usercontrol: < UserControl.Template > < ControlTemplate > < 网格 > < 边框 名称 = border BorderBrush = Gray 已移除 = WhiteSmoke BorderThickness = 3 CornerRadius = 20,0,40,0 > < Border.Effect > < DropShadowEffect 颜色 = 黑色 ShadowDepth = 7.5 方向 = 315 / > < / Border.Effect > < / Border > < 边框 名称 = 背景 保证金 = {绑定BorderThickness ,ElementName = border} CornerRadius = 20,0,40,0 已删除 = WhiteSmoke / > < 网格 > < Grid.RowDefinitions > < RowDefinition 高度 = 自动 / > < RowDefinition / > < / Grid.RowDefinitions > < 网格 VerticalAlignment = Top 保证金 = {绑定BorderThickness ,ElementName = border} 高度 = 20 > < Grid.ColumnDefinitions > < ColumnDefinition / > < ColumnDefinition 宽度 = 35 / > < ColumnDefinition 宽度 = 35 / > < ColumnDefinition 宽度 = 35\" / > < / Grid.ColumnDefinitions > < TextBlock 保证金 = 10,1 FontWeight = 粗体 FontStyle = 斜体 VerticalAlignment = 中心 文字 = {Binding Title,ElementName = userControl} / > < 按钮 Grid.Column = 3 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black Foreground = 红色 内容 = ? 点击 = close_Click 工具提示 = 关闭 / > < 按钮 Grid.Column = 2 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black 内容 = ? 点击 = maximize_Click 工具提示 = 最大化 / > < 按钮 Grid.Column = 1 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black 内容 = _ 点击 = minimize_Click 工具提示 = 最小化 / > < / Grid > < ContentControl 内容 = {Binding Content,ElementName = userControl} 保证金 = 5 Grid.Row = 1 / > < / Grid > < / Grid > < / ControlTemplate > < / UserControl.Template > 问题是WPF不允许使用基于XAML的控件作为根(我创建了一个UserControl,但我替换了root 'UserControl'与c:MyUserControl),所以我需要将其转换为基于代码的控制。 你能帮我吗?因为我试过了,但问题是设置事件和RowDefinitions。 感谢您的帮助,对不起我的英语不好。解决方案 关于你的代码似乎有些东西,这里是我正在研究的项目的模板: < style targettype = {x:Type local:TestBlockControl} > < setter property = 模板 > < setter.value > < controltemplate targettype = {x:输入local:TestBlockControl} > < grid cliptobounds = False > < / grid > < / controltemplate > < / setter.value > < / setter > < / style > 如您所见,我正在为名为TestBlockControl的用户控件设置默认样式。它应该对任何派生类型控件都一样,但是你不应该为你的模板定位UserControl,因为它会尝试将该模板应用于所有用户控件。这可能听起来像你想要的,但相信我,它不是。你需要定位你的MyUserControl类型。 你的代码看起来应该更像: < style targettype = {x:输入local:MyUserControl } > < setter property = 模板 > < setter.value > < controltemplate targettype = {x:输入local:MyUserControl} > < grid > < border name = border borderbrush = 灰色 已删除 = WhiteSmoke borderthickness = 3 cornerradius = 20,0,40 ,0 > < border.effect > < dropshadoweffect color = 黑色 shadowdepth = 7.5 方向 = 315 / > < /border.effect> < / border > < border 名称 = background margin = {绑定BorderThickness,ElementName = border} cornerradius = 20,0,40,0 已移除 = WhiteSmoke / > < grid > < grid.rowdefinitions > < rowdefinition height = 自动 / > < rowdefinition / > < / grid.rowdefinitions > < grid verticalalignment = Top margin = {Binding BorderThickness,ElementName = border} height = 20 > < grid.columndefinitions > < columndefinition / > < columndefinition width = 35 / > < columndefinition width = 35 / > < columndefinition width = 35 / > < / grid.columndefinitions > < textblock margin = 10,1 fontweight = 粗体 fontstyle = 斜体 verticalalignment = 中心 text = {Binding Title,ElementName = userControl} / > < 按钮 Grid.Column = 3 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black 前景 = 红色 内容 = ? 点击 = close_Click 工具提示 = 关闭 / > < 按钮 Grid.Column = 2 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black 内容 = ? 点击 = maximize_Click 工具提示 = 最大化 / > < 按钮 Grid.Column = 1 保证金 = 1 VerticalContentAlignment = 中心 FontWeight = ExtraBold FontFamily = Arial Black 内容 = _ 点击 = minimize_Click 工具提示 =\"Minimize\" /> </grid> <contentcontrol content=\"{Binding C ontent, ElementName=userControl}\" margin=\"5\" grid.row=\"1\" /> </grid> </grid> </controltemplate> </setter.value> </setter> </style> This style goes in your Themes/Generic.xaml file or you need to reference it in that file by using the MergedDictionaries key at the top.I solved this on my own by creating the template in the constructor of my control.. It was not easy but I did it.. Here the code I wrote. this.DataContext = this; var template = new ControlTemplate(); var mainGrid = new FrameworkElementFactory(typeof(Grid)); var borderTemplate = new FrameworkElementFactory(typeof(Border)); borderTemplate.SetValue(Border.BorderBrushProperty, Brushes.Gray); borderTemplate.SetValue(Border.BorderThicknessProperty, new Thickness(3)); borderTemplate.SetValue(Border.BackgroundProperty, Brushes.WhiteSmoke); borderTemplate.SetBinding(Border.CornerRadiusProperty, new Binding(\"CornerRadius\")); borderTemplate.SetValue(Border.EffectProperty, new DropShadowEffect() { Color = Colors.Black, ShadowDepth = 7.5, Direction = 315 }); var gridTemplate = new FrameworkElementFactory(typeof(Grid)); gridTemplate.SetValue(Grid.MarginProperty, new Thickness(3)); var titleColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); var minimizeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); minimizeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var maximizeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); maximizeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var closeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); closeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var titleRowTemplate = new FrameworkElementFactory(typeof(RowDefinition)); titleRowTemplate.SetValue(RowDefinition.HeightProperty, new GridLength(20)); var contentRowTemplate = new FrameworkElementFactory(typeof(RowDefinition)); var titleTemplate = new FrameworkElementFactory(typeof(TextBlock)); titleTemplate.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); titleTemplate.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold); titleTemplate.SetValue(TextBlock.FontStyleProperty, FontStyles.Italic); titleTemplate.SetBinding(TextBlock.TextProperty, new Binding(\"Title\")); titleTemplate.SetValue(TextBlock.MarginProperty, new Thickness(10, 0, 0, 0)); var minimizeTemplate = new FrameworkElementFactory(typeof(Button)); minimizeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); minimizeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); minimizeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily(\"Arial Black\")); minimizeTemplate.SetValue(Grid.ColumnProperty, 1); minimizeTemplate.SetValue(Button.ContentProperty, \"_\"); minimizeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.minimize_Click)); var maximizeTemplate = new FrameworkElementFactory(typeof(Button)); maximizeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); maximizeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); maximizeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily(\"Arial Black\")); maximizeTemplate.SetValue(Grid.ColumnProperty, 2); maximizeTemplate.SetValue(Button.ContentProperty, \"☐\"); maximizeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.maximize_Click)); var closeTemplate = new FrameworkElementFactory(typeof(Button)); closeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); closeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); closeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily(\"Arial Black\")); closeTemplate.SetValue(Grid.ColumnProperty, 3); closeTemplate.SetValue(Button.ContentProperty, \"✘\"); closeTemplate.SetValue(Button.ForegroundProperty, Brushes.Red); closeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.close_Click)); var contentTemplate = new FrameworkElementFactory(typeof(ContentControl)); contentTemplate.SetValue(Grid.RowProperty, 1); contentTemplate.SetValue(Grid.ColumnSpanProperty, 4); contentTemplate.SetValue(ContentControl.MarginProperty, new Thickness(5)); contentTemplate.SetBinding(ContentControl.ContentProperty, new Binding(\"Content\")); gridTemplate.AppendChild(titleColTemplate); gridTemplate.AppendChild(minimizeColTemplate); gridTemplate.AppendChild(maximizeColTemplate); gridTemplate.AppendChild(closeColTemplate); gridTemplate.AppendChild(titleRowTemplate); gridTemplate.AppendChild(contentRowTemplate); gridTemplate.AppendChild(titleTemplate); gridTemplate.AppendChild(minimizeTemplate); gridTemplate.AppendChild(maximizeTemplate); gridTemplate.AppendChild(closeTemplate); gridTemplate.AppendChild(contentTemplate); mainGrid.AppendChild(borderTemplate); mainGrid.AppendChild(gridTemplate); template.VisualTree = mainGrid; this.Template = template; Thank Ron Beyer for your kind help :)Hi everyone, I am trying to create a CustomUser control, because I need a base template for all userControls in my App (I'm trying to make a Single-Window-Application) with a sort of Window-in-Window, where the userControls are sort of windows in the MainWindow.xaml.To make it I tried to create my usercontrol in XAML and CS code, with the following template:<UserControl.Template> <ControlTemplate> <Grid > <Border Name="border" BorderBrush="Gray" removed="WhiteSmoke" BorderThickness="3" CornerRadius="20,0,40,0" > <Border.Effect> <DropShadowEffect Color="Black" ShadowDepth="7.5" Direction="315" /> </Border.Effect> </Border> <Border Name="background" Margin="{Binding BorderThickness, ElementName=border}" CornerRadius="20,0,40,0" removed="WhiteSmoke" /> <Grid > <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <Grid VerticalAlignment="Top" Margin="{Binding BorderThickness, ElementName=border}" Height="20"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="35"/> <ColumnDefinition Width="35"/> <ColumnDefinition Width="35"/> </Grid.ColumnDefinitions> <TextBlock Margin="10,1" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Center" Text="{Binding Title, ElementName=userControl}" /> <Button Grid.Column="3" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Foreground="Red" Content="?" Click="close_Click" ToolTip="Close" /> <Button Grid.Column="2" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Content="?" Click="maximize_Click" ToolTip="Maximize" /> <Button Grid.Column="1" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Content="_" Click="minimize_Click" ToolTip="Minimize" /> </Grid> <ContentControl Content="{Binding Content, ElementName=userControl}" Margin="5" Grid.Row="1" /> </Grid> </Grid> </ControlTemplate></UserControl.Template>the problem is that WPF doesn't allow to use XAML-based control as roots (I create a UserControl but I replace root 'UserControl' with c:MyUserControl), so I need to convert it into Code-based control.Can you please help me? Because I tryied, but the problem is setting events and RowDefinitions.Thanks for your kind help and sorry for my bad English. 解决方案 Something seems off about your code, here is what a template looks like for a project I'm working on: <style targettype="{x:Type local:TestBlockControl}"> <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type local:TestBlockControl}"> <grid cliptobounds="False"></grid></controltemplate></setter.value></setter></style>As you see, I'm setting the default style for a user control named TestBlockControl. It should work the same for any derived type controls but you should not target UserControl for your template since it will try to apply that template to all user controls. That may sound like what you want, but trust me, its not. You will want to target your MyUserControl type.Your code should look something more like:<style targettype="{x:Type local:MyUserControl}"> <setter property="Template"> <setter.value> <controltemplate targettype="{x:Type local:MyUserControl}"> <grid> <border name="border" borderbrush="Gray" removed="WhiteSmoke" borderthickness="3" cornerradius="20,0,40,0"> <border.effect> <dropshadoweffect color="Black" shadowdepth="7.5" direction="315" /> </border.effect> </border> <border name="background" margin="{Binding BorderThickness, ElementName=border}" cornerradius="20,0,40,0" removed="WhiteSmoke" /> <grid> <grid.rowdefinitions> <rowdefinition height="Auto" /> <rowdefinition /> </grid.rowdefinitions> <grid verticalalignment="Top" margin="{Binding BorderThickness, ElementName=border}" height="20"> <grid.columndefinitions> <columndefinition /> <columndefinition width="35" /> <columndefinition width="35" /> <columndefinition width="35" /> </grid.columndefinitions> <textblock margin="10,1" fontweight="Bold" fontstyle="Italic" verticalalignment="Center" text="{Binding Title, ElementName=userControl}" /> <Button Grid.Column="3" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Foreground="Red" Content="?" Click="close_Click" ToolTip="Close" /> <Button Grid.Column="2" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Content="?" Click="maximize_Click" ToolTip="Maximize" /> <Button Grid.Column="1" Margin="1" VerticalContentAlignment="Center" FontWeight="ExtraBold" FontFamily="Arial Black" Content="_" Click="minimize_Click" ToolTip="Minimize" /> </grid> <contentcontrol content="{Binding Content, ElementName=userControl}" margin="5" grid.row="1" /> </grid> </grid> </controltemplate> </setter.value> </setter></style>This style goes in your Themes/Generic.xaml file or you need to reference it in that file by using the MergedDictionaries key at the top.I solved this on my own by creating the template in the constructor of my control.. It was not easy but I did it..Here the code I wrote.this.DataContext = this; var template = new ControlTemplate(); var mainGrid = new FrameworkElementFactory(typeof(Grid)); var borderTemplate = new FrameworkElementFactory(typeof(Border)); borderTemplate.SetValue(Border.BorderBrushProperty, Brushes.Gray); borderTemplate.SetValue(Border.BorderThicknessProperty, new Thickness(3)); borderTemplate.SetValue(Border.BackgroundProperty, Brushes.WhiteSmoke); borderTemplate.SetBinding(Border.CornerRadiusProperty, new Binding("CornerRadius")); borderTemplate.SetValue(Border.EffectProperty, new DropShadowEffect() { Color = Colors.Black, ShadowDepth = 7.5, Direction = 315 }); var gridTemplate = new FrameworkElementFactory(typeof(Grid)); gridTemplate.SetValue(Grid.MarginProperty, new Thickness(3)); var titleColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); var minimizeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); minimizeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var maximizeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); maximizeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var closeColTemplate = new FrameworkElementFactory(typeof(ColumnDefinition)); closeColTemplate.SetValue(ColumnDefinition.WidthProperty, new GridLength(35)); var titleRowTemplate = new FrameworkElementFactory(typeof(RowDefinition)); titleRowTemplate.SetValue(RowDefinition.HeightProperty, new GridLength(20)); var contentRowTemplate = new FrameworkElementFactory(typeof(RowDefinition)); var titleTemplate = new FrameworkElementFactory(typeof(TextBlock)); titleTemplate.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); titleTemplate.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold); titleTemplate.SetValue(TextBlock.FontStyleProperty, FontStyles.Italic); titleTemplate.SetBinding(TextBlock.TextProperty, new Binding("Title")); titleTemplate.SetValue(TextBlock.MarginProperty, new Thickness(10, 0, 0, 0)); var minimizeTemplate = new FrameworkElementFactory(typeof(Button)); minimizeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); minimizeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); minimizeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily("Arial Black")); minimizeTemplate.SetValue(Grid.ColumnProperty, 1); minimizeTemplate.SetValue(Button.ContentProperty, "_"); minimizeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.minimize_Click)); var maximizeTemplate = new FrameworkElementFactory(typeof(Button)); maximizeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); maximizeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); maximizeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily("Arial Black")); maximizeTemplate.SetValue(Grid.ColumnProperty, 2); maximizeTemplate.SetValue(Button.ContentProperty, "☐"); maximizeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.maximize_Click)); var closeTemplate = new FrameworkElementFactory(typeof(Button)); closeTemplate.SetValue(Button.MarginProperty, new Thickness(1)); closeTemplate.SetValue(Button.FontWeightProperty, FontWeights.ExtraBold); closeTemplate.SetValue(Button.FontFamilyProperty, new FontFamily("Arial Black")); closeTemplate.SetValue(Grid.ColumnProperty, 3); closeTemplate.SetValue(Button.ContentProperty, "✘"); closeTemplate.SetValue(Button.ForegroundProperty, Brushes.Red); closeTemplate.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.close_Click)); var contentTemplate = new FrameworkElementFactory(typeof(ContentControl)); contentTemplate.SetValue(Grid.RowProperty, 1); contentTemplate.SetValue(Grid.ColumnSpanProperty, 4); contentTemplate.SetValue(ContentControl.MarginProperty, new Thickness(5)); contentTemplate.SetBinding(ContentControl.ContentProperty, new Binding("Content")); gridTemplate.AppendChild(titleColTemplate); gridTemplate.AppendChild(minimizeColTemplate); gridTemplate.AppendChild(maximizeColTemplate); gridTemplate.AppendChild(closeColTemplate); gridTemplate.AppendChild(titleRowTemplate); gridTemplate.AppendChild(contentRowTemplate); gridTemplate.AppendChild(titleTemplate); gridTemplate.AppendChild(minimizeTemplate); gridTemplate.AppendChild(maximizeTemplate); gridTemplate.AppendChild(closeTemplate); gridTemplate.AppendChild(contentTemplate); mainGrid.AppendChild(borderTemplate); mainGrid.AppendChild(gridTemplate); template.VisualTree = mainGrid; this.Template = template;Thank Ron Beyer for your kind help :) 这篇关于在代码中转换此XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-04 10:09