C# 带有定义网格的WPF选项卡项样式(布局)
我创造了这样的风格:C# 带有定义网格的WPF选项卡项样式(布局),c#,wpf,C#,Wpf,我创造了这样的风格: <Window.Resources> <Style x:Key="WejStyle" TargetType="{x:Type TabItem}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Header" Value="Wej"/> <Setter Property="
<Window.Resources>
<Style x:Key="WejStyle" TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Header" Value="Wej"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<TextBlock TextWrapping="Wrap" Text="Adress(hex)" FontSize="15" Margin="10,10,396,355"/>
<ComboBox x:Name="WejAdress" HorizontalAlignment="Left" Margin="134,11,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem>3A</ComboBoxItem>
<ComboBoxItem>3B</ComboBoxItem>
<ComboBoxItem>3C</ComboBoxItem>
<ComboBoxItem>3D</ComboBoxItem>
<ComboBoxItem>3E</ComboBoxItem>
<ComboBoxItem>3F</ComboBoxItem>
</ComboBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
但它只在标题和网格中返回System.Windows.Style
有人能告诉我我做错了什么吗
编辑:
我更改AddWej()
和xaml中的代码,来自:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
到
它可以工作。试试
var style=(style)this.FindResource(“WejStyle”)代码>返回异常“Resource”WejStyle“not found.”new Resources().FindResource(…)
没有意义。您可以在窗口资源中定义它,因此需要该窗口引用。AddWej
方法在哪里?什么时候,怎样称呼它?我们需要看到更多的代码。我在其他wpf窗口中定义了样式,当我将此样式移动到Main窗口时,我使用它“this.FindResource(“WejStyle”);“它不会抛出错误,但我有相同的结果”System.Windows.style“。
public void AddWej()
{
var resources = new Resources();
var style = (Style)resources.FindResource("WejStyle");
var tabItem = new TabItem();
tabItem.Style = style;
tabItem.Header = "Wej";
TabCards.Items.Add(tabItem);
//dodanie
cards.Add(new CardWej() { });
}
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>