用c#代码编程的WPF控制模板

本文关键字:控制 WPF 代码 编程 | 更新日期: 2023-09-27 18:12:27

我试图为按钮创建一个样式,特别是要更改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;
}

用c#代码编程的WPF控制模板

你似乎把Button在按钮的模板。这个内部按钮与外部按钮不一样,它们也不会以任何方式联系在一起。通常,我会期望例如BorderBackground属性绑定到模板父节点的地方。它的默认值将被设置为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);

相当于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的行为,直到你手动添加它。