WPF控制模板以编程方式使用C#代码

时间:2015-09-13 07:52:39

标签: c# wpf templates controls

我尝试为按钮创建样式,尤其是更改IsMouseOver样式(必须通过控件模板覆盖)。我尝试使用此代码(我必须使用c#,没有xaml),但按钮只是变成黄色,IsMouseOver没有做任何事情。

private void CreateElement(int i)
{
    UIElementOut[i] = new Button();
    var uiElement = (Button)UIElementOut[i];
    uiElement.Width = 100;
    uiElement.Height = 100;
    uiElement.VerticalAlignment = VerticalAlignment.Center;
    uiElement.HorizontalAlignment = HorizontalAlignment.Center;
    uiElement.Content = TextIn[i];
    uiElement.FontFamily = new FontFamily(FFontInput[i]);
    uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);

    Style MyButtonStyle = new Style();
    ControlTemplate templateButton = new ControlTemplate(typeof(Button));
    FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Button));
    elemFactory.Name = "myButton";
    elemFactory.SetValue(Button.BackgroundProperty, Brushes.Yellow);
    templateButton.VisualTree = elemFactory;
    elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));

    Trigger templateTrigger = new Trigger { Property = Button.IsPressedProperty, Value = true };
    templateTrigger.Setters.Add(new Setter { Property = Button.ForegroundProperty, Value = Brushes.Violet });
    templateTrigger.Setters.Add(new Setter { Property = Button.BackgroundProperty, Value = Brushes.Red });
    templateButton.Triggers.Add(templateTrigger);

    Setter setter = new Setter { TargetName = "myButton", Property = Button.TemplateProperty, Value = templateButton };
    MyButtonStyle.Setters.Add(setter);
    uiElement.Style = MyButtonStyle;
    uiElement.Template = templateButton;
}

1 个答案:

答案 0 :(得分:2)

您似乎将Button放在按钮的模板中。此内部按钮与外部按钮不同,也不会以任何方式链接。通常我希望例如BorderBackground属性绑定到模板化父级的位置。其默认值将设置为Style的设置器,并由Style的触发器

更改
Style MyButtonStyle = new Style();
ControlTemplate templateButton = new ControlTemplate(typeof(Button));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Border));
elemFactory.SetBinding(Border.BackgroundProperty, new Binding { RelativeSource = RelativeSource.TemplatedParent, Path = new PropertyPath("Background") });
templateButton.VisualTree = elemFactory;
elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
MyButtonStyle.Setters.Add(new Setter { Property = Button.BackgroundProperty, Value = Brushes.Yellow });
MyButtonStyle.Setters.Add(new Setter { Property = Button.TemplateProperty, Value = templateButton });
Trigger styleTrigger = new Trigger { Property = Button.IsMouseOverProperty, Value = true };
styleTrigger.Setters.Add(new Setter { Property = Button.ForegroundProperty, Value = Brushes.Violet });
styleTrigger.Setters.Add(new Setter { Property = Button.BackgroundProperty, Value = Brushes.Red });
MyButtonStyle.Triggers.Add(styleTrigger);

相当于XAML中的相同代码

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Yellow"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="Violet"/>
        </Trigger>
    </Style.Triggers>
</Style>

但是,因为您更改Template,这有效地使按钮看起来和感觉,您需要知道您的Button赢得了默认Button&# 39;直到你手动添加它为止的行为。