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

lec*_*eur 1 c# wpf controls templates

我正在尝试为按钮创建样式,特别是更改 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;
}
Run Code Online (Sandbox Code Playgroud)

dko*_*ozl 6

您似乎将其放入Button按钮的模板中。该内部按钮与外部按钮不同,它们也不会以任何方式链接。通常我会期望Border在那个地方Background将属性绑定到模板化父级。它的默认值将被设置为 Style 的 setter 并由 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);
Run Code Online (Sandbox Code Playgroud)

这相当于 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>
Run Code Online (Sandbox Code Playgroud)

但是,由于您进行了更改Template,这实际上使按钮的外观和感觉发生了变化,因此您需要注意,在手动添加之前,您Button不会具有默认的行为。Button