none
请问:XAML 和 C#代码 间,如何互相转化? RRS feed

  • 问题

  • 比如,这是一个我下载的圆角按钮的示例代码,但我想要它的C#代码格式:

    <Button Content="地图" Height="50" Margin="555,282,466,393" Width="50">
                    <Button.Template>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="3,13,3,13" Name="PART_Background">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="White" Offset="0.0" />
                                        <GradientStop Color="Silver" Offset="0.5" />
                                        <GradientStop Color="White" Offset="0.0" />
                                    </LinearGradientBrush>
                                </Border.Background>
                                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="UIElement.IsMouseOver" Value="True">
                                    <Setter Property="Border.Background" TargetName="PART_Background">
                                        <Setter.Value>
                                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                <GradientStop Color="Silver" Offset="0.0" />
                                                <GradientStop Color="White" Offset="0.5" />
                                                <GradientStop Color="Silver" Offset="0.0" />
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="ButtonBase.IsPressed" Value="True">
                                    <Setter Property="UIElement.Effect">
                                        <Setter.Value>
                                            <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.6" RenderingBias="Performance" ShadowDepth="0" />
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>
                </Button>

    我尤其不知道,这段XAML中的 border 赋值给 ControlTemplate的哪个属性:

    <ControlTemplate TargetType="{x:Type Button}">
                            <Border BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="0" CornerRadius="3,13,3,13" Name="PART_Background">
    请解答以上问题后,最好还能告诉我:如何学会 XAML 转换到 C#代码 的方法,因为网络上大多示例都是用XAML写的。转换的事情,很头大。

    2014年10月15日 7:38

答案

  • 你好,

    >>我尤其不知道,这段XAML中的 border 赋值给 ControlTemplate的哪个属性

    你需要使用FrameworkElementFactory 去协助创建控件模板:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelementfactory(v=vs.110).aspx

    以下是我根据你的XAML转后后的后台代码:

    //Template
    ControlTemplate template = new ControlTemplate(typeof(Button));
    FrameworkElementFactory borderElement = new FrameworkElementFactory(typeof(Border));
    borderElement.Name = "PART_Background";
    borderElement.SetValue(Border.BorderBrushProperty, new TemplateBindingExtension(Control.BorderBrushProperty));
    borderElement.SetValue(Border.BorderThicknessProperty, new Thickness(0));
    borderElement.SetValue(Border.CornerRadiusProperty, new CornerRadius(3, 13, 3, 13));
    
    LinearGradientBrush lgbrush = new LinearGradientBrush() { EndPoint = new Point(0, 1), StartPoint = new Point(0, 0) };
    lgbrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
    lgbrush.GradientStops.Add(new GradientStop(Colors.Silver, 0.5));
    lgbrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
    borderElement.SetValue(Border.BackgroundProperty, lgbrush);
    
    //ContentPresenter
    FrameworkElementFactory cpElement = new FrameworkElementFactory(typeof(ContentPresenter));
    cpElement.SetValue(ContentPresenter.ContentProperty, new TemplateBindingExtension(ContentControl.ContentProperty));
    cpElement.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
    cpElement.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
    
    borderElement.AppendChild(cpElement);
    template.VisualTree = borderElement;
    
    //Trigger
    Trigger trigger1 = new Trigger();
    trigger1.Property = UIElement.IsMouseOverProperty;
    trigger1.Value = true;
    Setter setter1 = new Setter();
    setter1.TargetName = "PART_Background";
    setter1.Property = Border.BackgroundProperty;
    LinearGradientBrush lgbrush2 = new LinearGradientBrush() { EndPoint = new Point(0, 1), StartPoint = new Point(0, 0) };
    lgbrush2.GradientStops.Add(new GradientStop(Colors.Silver, 0.0));
    lgbrush2.GradientStops.Add(new GradientStop(Colors.White, 0.5));
    lgbrush2.GradientStops.Add(new GradientStop(Colors.Silver, 0.0));
    setter1.Value = lgbrush2;
    trigger1.Setters.Add(setter1);
    
    template.Triggers.Add(trigger1);
    
    //Trigger
    Trigger trigger2 = new Trigger();
    trigger2.Property = ButtonBase.IsPressedProperty;
    trigger2.Value = true;
    Setter setter2 = new Setter();
    setter2.Property = UIElement.EffectProperty;
    setter2.Value = new DropShadowEffect() { BlurRadius=10, Color=Colors.Black, Direction= 0, Opacity=0.6, RenderingBias=RenderingBias.Performance, ShadowDepth=0 };
    trigger2.Setters.Add(setter2);
    
    template.Triggers.Add(trigger2);
    
    button1.Template = template; //设置模板

    XAML:

    <Button Name="button1" Content="地图" Height="50" Width="50" />

    >>如何学会 XAML 转换到 C#代码 的方法,因为网络上大多示例都是用XAML写的

    以我的经验,可以套用网络上一个一些关于创建后台模板的代码,根据你的需求修改, MSDN英文论坛也有很多类似问题:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/ddb7fd77-dea1-40f5-a5b7-30d35ecda70e/creating-a-control-template-with-code-behind?forum=wpf
    http://social.msdn.microsoft.com/Forums/en-US/74ef9039-16e7-4e24-945e-1196a4ad9bde/how-to-write-style-code-from-xaml-in-code-behind?forum=wpf

    如果有任何问题,欢迎来论坛贴出你的代码,我们帮助你解决细小问题:)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 OpenNovo 2014年10月16日 13:32
    2014年10月16日 7:45
    版主