相关疑难解决方法(0)

在StackPanel上填充?

我试图设置StackPanel的填充,但没有这样的属性.我尝试过StackPanel.Border,但也没有这样的属性.

有任何想法吗?

wpf layout xaml padding stackpanel

67
推荐指数
3
解决办法
4万
查看次数

在StackPanel中设置项目间距的简便方法是什么?

有没有一种简单的方法在StackPanel内的项目之间设置默认空间,所以我不必在每个项目上设置Margin属性?

c# wpf xaml stackpanel

19
推荐指数
2
解决办法
2万
查看次数

如果没有WPF从Aero恢复到Classic,如何更改按钮的默认样式?

我已将PresentationFramework.Aero添加到我的App.xaml合并词典中,如...

<Application
    x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                    Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
                <ResourceDictionary
                     Source="pack://application:,,,/WPFToolkit;component/Themes/Aero.NormalColor.xaml" />
                <ResourceDictionary
                    Source="/CommonLibraryWpf;component/ResourceDictionaries/ButtonResourceDictionary.xaml" />
                    <!-- Note, ButtonResourceDictionary.xaml is defined in an external class library-->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

我正在尝试稍微修改按钮的默认外观.我把这个样式放在我的ButtonResourceDictionary中:

<Style TargetType="Button">
    <Setter Property="Padding" Value="3" />
    <Setter Property="FontWeight" Value="Bold" />
</Style>
Run Code Online (Sandbox Code Playgroud)

所有按钮现在都有正确的填充和粗体文本,但它们看起来是"经典",而不是"Aero".如何修复此样式,以便我的按钮看起来都是Aero,但也有这些微小的变化?我宁愿不必Style为每个按钮设置属性.

更新

我应该首先提到这一点,但如果我尝试使用BasedOn,如下所示,我得到一个StackOverflowException:

<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
    <Setter Property="Padding" Value="3" />
    <Setter Property="FontWeight" Value="Bold" />
</Style>
Run Code Online (Sandbox Code Playgroud)

这通常会起作用,但不会合并到Aero词典中.如果我对这些词典进行评论,则异常消失.

更新2

如果我添加一个x:Key属性并手动设置样式,它可以正常工作(带有填充和粗体的Aero样式),但正如我所说,我更喜欢该样式全局自动应用于所有按钮.

更新3

我刚刚发现了一种新的皱纹.在我的应用程序中,ButtonResourceDictionary.xaml被放置在类库中(即,在外部项目中).如果我将此文件移动到本地文件夹,一切正常.因此,问题似乎是引用各种外部资源字典引起的不良交互.我正在纠正我的App.xaml代码片段(上面),以反映ButtonResourceDictionary实际上是在外部定义的.

wpf xaml themes

6
推荐指数
1
解决办法
9101
查看次数

在ItemsControl中的每个项目周围包裹一些东西

假设我有一组不同类的对象.每个类在资源文件中都有UserControl DataTemplated.

现在我想使用ItemsControl来显示集合,但我希望每个项目周围都有一个Border或Expander.

我希望这样的东西能起作用:

<ItemsControl ItemsSource="{Binding MyObjects}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Black" BorderThickness="3">
                <ContentPresenter/>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

但是ContentPresenter似乎选择了ItemTemplate,因为我得到了堆栈溢出.

如何在ItemTemplate中获取每个Item的DataTemplate?

wpf datatemplate itemtemplate itemscontrol

6
推荐指数
1
解决办法
3732
查看次数

WPF 样式基于当前上下文中的父样式

比如说,我有一个TextBox“TextBoxStyleBase”的默认样式。然后,我定义一个DataGrid样式,该样式具有TextBox基于该基本样式的自己的样式,并定义另一个边框颜色。

在 a 内部的某个地方,DataGrid我想定义另一种TextBox样式,但继承于 style 中定义的样式DataGrid

有没有办法使样式继承当前为当前“上下文”中的特定控件定义的样式?

编辑:

为了更清楚地说明,这是我所拥有的:

<!-- explicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
    <Setter Property="FontSize" Value="16"/>
</Style>

<!-- implicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}"/>

<!-- DataGrid style changing inner TextBox style -->
<Style TargetType="{x:Type DataGrid}">
    <Style.Resources>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}">
            <Setter Property="FontSize" Value="20"/>
        </Style>
        <!-- since TextBox has defined implicit style this would be equivalent …
Run Code Online (Sandbox Code Playgroud)

wpf styles basedon

1
推荐指数
1
解决办法
2944
查看次数