简体   繁体   English

以编程方式添加WPF UserControl没有样式

[英]Adding a WPF UserControl programmatically has no style

I'm trying to create a button-like UserControl which consists of a border and a label. 我正在尝试创建一个类似按钮的UserControl,它由边框和标签组成。 When adding this UserControl directly in the XAML of MainWindow, it renders correctly (it is put in a WrapPanel), but if I add it programmatically it doesn't look the same at all. 直接在MainWindow的XAML中添加此UserControl时,它可以正确呈现(将其放在WrapPanel中),但是如果我以编程方式添加它,则看起来根本不一样。 There is no border, no background color, the text size of the label is wrong and so is the text color. 没有边框,没有背景色,标签的文本大小不正确,文本颜色也是如此。

This is my user control: 这是我的用户控件:

<UserControl x:Class="Jishi.SonosPartyMode.UserControls.Player"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="300"
             Width="200"
             Height="100">
    <Border BorderBrush="#086EAA" CornerRadius="8,8,8,8"  BorderThickness="4" Margin="15" Padding="15px" VerticalAlignment="Center" Background="#0A4069">
        <Label Name="PlayerName" TextElement.Foreground="White" TextElement.FontSize="20px">asdasdasd</Label>
    </Border>
</UserControl>

when adding it, I just invoke it like this: 添加它时,我只是这样调用它:

var button = new Player { Content = player.Properties.RoomName, DataContext = player.Properties.UDN };
PlayerList.Children.Add( button );

PlayerList is the actual WrapPanel and Player is my UserControl. PlayerList是实际的WrapPanel,Player是我的UserControl。 I have tried finding information regarding this, but I don't find anything. 我尝试查找有关此的信息,但未找到任何信息。 If you have another approach that I can take, please come with suggestions. 如果您可以采用其他方法,请提出建议。 All I want is a clickable area with some rounded corners that can contain text (one or more rows). 我想要的是一个带有一些圆角的可单击区域,该圆角可以包含文本(一个或多个行)。

I can apply styles programatically, but the styles defined in the xaml for the UserControl isn't preserved (Border, Margins, colors etc). 我可以通过编程方式应用样式,但是不保留在xaml中为UserControl定义的样式(边框,边距,颜色等)。

First of all, you don't need to create a custom control for this, you can easily do 首先,您不需要为此创建自定义控件,您可以轻松地

var button = new Border { *border properties*, Content = new Lable {Content="dfsdfsdfsd"}};

And if you use PlayerList.Children.Add( button ); 并且如果您使用PlayerList.Children.Add(button); then it adds to the end of Wrappanel and in XAML code you add it not as the last element (maybe).. 然后将其添加到Wrappanel的末尾,并在XAML代码中将其添加为最后一个元素(也许不是)。

And the last idea is that you lost some properties that you added in XAML when test it (like aligment, margin, etc.) 最后一个想法是,您在测试XAML时会丢失一些在XAML中添加的属性(例如,匹配度,边距等)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM