WPF XAML本地(儿童)样式被覆盖。为什么?

发布于 2025-02-02 17:06:33 字数 7641 浏览 2 评论 0原文

我写了一个简单的对话框(XAML/WPF)和一个测试应用程序,对话框看起来不错。特别是,ListView中的按钮具有圆角。我已经发布了一张图片和下面的代码。 问题?当我在更大的程序中使用此对话框(代码库太大而无法共享)时,圆角和其他样式就消失了。我强烈怀疑较大的计划中的某些事情优先于我的本地工作。也许是按钮或类似东西的全球风格?

  1. 我想了解发生了什么。大概是主应用程序中的某些内容优先于我的XAML工作?
  2. 我想知道是否有一种方法可以说“不要从应用程序本身继承样式。宁愿使用wpf默认值,除非我覆盖它们。”假设这是问题。”

请参阅图片(注意圆角)

”在此处输入图像描述

从实际主应用程序调用它的情况下,请参阅图片,而不是测试应用程序

通知,尤其是缺少圆角。我生产圆角的工作已经消失了!另外,在测试应用程序中,悬停在按钮上显示蓝色,我认为这是默认的(我没有这样做)。当从主应用程序调用时,没有这种悬停效应。我怀疑主应用程序摆脱了某个地方。

这是简单的对话框XAML

<Window x:Class="FirmsDialog"
             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" 
             xmlns:local="clr-namespace:Dialogs"
                
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="400"
        Width="390" Height="720" BorderBrush="LightGray"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
         x:Name="FirmsViewDlg" Loaded="FirmsViewDlg_Loaded"
        >

    <Window.Resources>
        <FontFamily x:Key="AvenirNextforCompany">
            pack://application:,,,/Assets/Fonts/#AvenirNextforCompany
        </FontFamily>
        <local:ReverseObjectToBool x:Key="ReverseObjectToBoolConverter" />

        <local:ObjectToBool x:Key="ObjectToBoolConverter" />
       
        
        

        
        <!-- New style -->
        <Style x:Key="StyleListViewItem" TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
            
        </Style>

        <!-- Existing style -->
        <Style x:Key="StyleListView" TargetType="ListView">
            <Setter Property="ItemContainerStyle" Value="{StaticResource StyleListViewItem}"/>
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <!-- ... -->
        </Style>
    </Window.Resources>

    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Content="Select Company" HorizontalAlignment="Center" FontSize="20px" Foreground="#393a3d" FontFamily="{StaticResource AvenirNextforCompany}" FontWeight="Normal"  ></Label>
        <Label Grid.Row="1" Content="Accountant companies" Margin="10,0" FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" Foreground="#8d9096" FontWeight="Normal"></Label>
        <ListView  BorderThickness="0" Grid.Row="2" ItemsSource="{Binding RealmMembershipInfo}"  SelectedItem="{Binding SelectedFirm}"   x:Name="realmListBox" 
                   HorizontalContentAlignment="Stretch"  HorizontalAlignment="Stretch" Height="Auto" Margin="0,0,0,0"
                   VerticalAlignment="Top"  FontWeight="Bold" FontSize="14" Background="White"              
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    BorderBrush="LightGray">
            
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Button  Click="Button_Click" 
                       
                         MinHeight="65" Padding="10,0,10,0"
                        Margin="0,0,0,0"            HorizontalContentAlignment="Left" HorizontalAlignment="Stretch"     BorderBrush="LightGray"    Background="White" Foreground="#393a3d"  FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" FontWeight="SemiBold"
                            
                        >
                        <Button.Resources>
                            <Style TargetType="Border">
                                <Setter Property="CornerRadius" Value="9"/>


                            </Style>

                        </Button.Resources>
                        <TextBlock Text="{Binding displayName}"  TextWrapping="Wrap" HorizontalAlignment="Stretch"  Foreground="#393a3d"  FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" FontWeight="Bold" >

                        </TextBlock>

                    </Button>
                </DataTemplate>
            </ListView.ItemTemplate>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Margin" Value="10,10,10,10"/>
                    <Setter Property="Padding" Value="0"/>
                    <Style.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter Property="Control.Background" Value="Transparent" />
                            <Setter Property="BorderBrush" Value="Transparent" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

    </Grid>
</Window>

简单程序以显示上面的XAML

using Dialogs;
using Models;
using System.Collections.Generic;
using System.Windows;

namespace TestApp
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Button_SelectFirm(object sender, RoutedEventArgs e)
      {
         // hack code to set up dialog
         RealmMembershipInfo realmInfo = new RealmMembershipInfo();
         realmInfo.realmMembershipInfo = new List<RealmMembershipItem>();

         RealmMembershipItem item = new RealmMembershipItem();
         item.displayName = "Company 1";        
         realmInfo.realmMembershipInfo.Add(item);

         item = new RealmMembershipItem();
         item.displayName = "Company2";         
         realmInfo.realmMembershipInfo.Add(item);

         

         FirmsDialog dlg = new FirmsDialog(realmInfo);
         dlg.ShowDialog();
         MessageBox.Show("Your picked firm: " + dlg.SelectedFirm);
         
      }

      private void Button_SelectClient(object sender, RoutedEventArgs e)
      {
        
      }
   }
}

I wrote a simple dialog (XAML/WPF) and a test app and the dialog looks fine. In particular the buttons in the ListView have rounded corners. I've posted a picture and the code below.
The problem? When I use this dialog inside a much larger program (codebase too large to share), the rounded corners and other styling is gone. I strongly suspect something in the larger program is taking precedence over my local work. Perhaps a global style for buttons or some such thing?

  1. I'd like to understand what is going on. Presumably something in the main app takes precedence over my xaml work?
  2. I'd like to know if there is a way to say "don't inherit styles from the app itself. Rather use WPF defaults unless I override them.", assuming that is the problem.

See picture (notice rounded corners)

enter image description here

See picture from when I call it from actual main application instead of test application

Notice in particular lack of rounded corners. My work to produce rounded corners is gone! Also, in the test app, hovering over a button shows a blue color which I assume is default (I didn't do it). When called from main app, no such hover effect. I suspect the main app gets rid of that somewhere.

enter image description here

Here's the simple dialog xaml

<Window x:Class="FirmsDialog"
             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" 
             xmlns:local="clr-namespace:Dialogs"
                
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="400"
        Width="390" Height="720" BorderBrush="LightGray"
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
         x:Name="FirmsViewDlg" Loaded="FirmsViewDlg_Loaded"
        >

    <Window.Resources>
        <FontFamily x:Key="AvenirNextforCompany">
            pack://application:,,,/Assets/Fonts/#AvenirNextforCompany
        </FontFamily>
        <local:ReverseObjectToBool x:Key="ReverseObjectToBoolConverter" />

        <local:ObjectToBool x:Key="ObjectToBoolConverter" />
       
        
        

        
        <!-- New style -->
        <Style x:Key="StyleListViewItem" TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
            
        </Style>

        <!-- Existing style -->
        <Style x:Key="StyleListView" TargetType="ListView">
            <Setter Property="ItemContainerStyle" Value="{StaticResource StyleListViewItem}"/>
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <!-- ... -->
        </Style>
    </Window.Resources>

    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Content="Select Company" HorizontalAlignment="Center" FontSize="20px" Foreground="#393a3d" FontFamily="{StaticResource AvenirNextforCompany}" FontWeight="Normal"  ></Label>
        <Label Grid.Row="1" Content="Accountant companies" Margin="10,0" FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" Foreground="#8d9096" FontWeight="Normal"></Label>
        <ListView  BorderThickness="0" Grid.Row="2" ItemsSource="{Binding RealmMembershipInfo}"  SelectedItem="{Binding SelectedFirm}"   x:Name="realmListBox" 
                   HorizontalContentAlignment="Stretch"  HorizontalAlignment="Stretch" Height="Auto" Margin="0,0,0,0"
                   VerticalAlignment="Top"  FontWeight="Bold" FontSize="14" Background="White"              
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    BorderBrush="LightGray">
            
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Button  Click="Button_Click" 
                       
                         MinHeight="65" Padding="10,0,10,0"
                        Margin="0,0,0,0"            HorizontalContentAlignment="Left" HorizontalAlignment="Stretch"     BorderBrush="LightGray"    Background="White" Foreground="#393a3d"  FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" FontWeight="SemiBold"
                            
                        >
                        <Button.Resources>
                            <Style TargetType="Border">
                                <Setter Property="CornerRadius" Value="9"/>


                            </Style>

                        </Button.Resources>
                        <TextBlock Text="{Binding displayName}"  TextWrapping="Wrap" HorizontalAlignment="Stretch"  Foreground="#393a3d"  FontFamily="{StaticResource AvenirNextforCompany}" FontSize="14px" FontWeight="Bold" >

                        </TextBlock>

                    </Button>
                </DataTemplate>
            </ListView.ItemTemplate>

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Margin" Value="10,10,10,10"/>
                    <Setter Property="Padding" Value="0"/>
                    <Style.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter Property="Control.Background" Value="Transparent" />
                            <Setter Property="BorderBrush" Value="Transparent" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

    </Grid>
</Window>

Simple program to show the Xaml above

using Dialogs;
using Models;
using System.Collections.Generic;
using System.Windows;

namespace TestApp
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Button_SelectFirm(object sender, RoutedEventArgs e)
      {
         // hack code to set up dialog
         RealmMembershipInfo realmInfo = new RealmMembershipInfo();
         realmInfo.realmMembershipInfo = new List<RealmMembershipItem>();

         RealmMembershipItem item = new RealmMembershipItem();
         item.displayName = "Company 1";        
         realmInfo.realmMembershipInfo.Add(item);

         item = new RealmMembershipItem();
         item.displayName = "Company2";         
         realmInfo.realmMembershipInfo.Add(item);

         

         FirmsDialog dlg = new FirmsDialog(realmInfo);
         dlg.ShowDialog();
         MessageBox.Show("Your picked firm: " + dlg.SelectedFirm);
         
      }

      private void Button_SelectClient(object sender, RoutedEventArgs e)
      {
        
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2025-02-09 17:06:33

您尚未在listView上设置样式。

<ListView Style={StaticResource StyleListView} ....

而且,您正在为项目范围内使用默认样式。您需要添加基本样式。

 <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" BasedOn={StaticResource StyleListViewItem}...

You haven't set the style on the Listview.

<ListView Style={StaticResource StyleListView} ....

And you are using the default style for the ItemsContainer. You need to add the a base style.

 <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" BasedOn={StaticResource StyleListViewItem}...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文