I want to set a default Margin of 3 on all the controls I put on all my windows and be able to override this value just on a really few number of items.
我想在我放在所有窗口上的所有控件上设置一个默认的3个边距,并且只能在很少的项目上覆盖这个值。
I've seen some approaches like doing styles but then I need to style everything, I would prefer something than can be done for all the controls together. I've seen other things like the MarginSetter but looks like it does not traverse subpanels. I want the Margin only on the controls I put at the window, nothing to do with the borders or other things of the visual tree.
我已经看过一些像做风格的方法,但后来我需要设计一切风格,我更喜欢一些比所有控件都可以完成的事情。我已经看过其他东西,比如MarginSetter,但看起来它不会遍历子面板。我只想在我放在窗口的控件上使用Margin,与视觉树的边框或其他东西无关。
Looks something pretty basic to me. Any ideas?
看起来对我来说非常基本。有任何想法吗?
Thanks in advance.
提前致谢。
3 个解决方案
#1
19
The only solution I can find is to apply the style to each of the controls you are using on the window (I know that's not quite what you want). If you're only using a few different control types it's not too onerous to do something like this:
我能找到的唯一解决方案是将样式应用于您在窗口上使用的每个控件(我知道这不是您想要的)。如果您只使用几种不同的控件类型,那么执行以下操作并不会太繁琐:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- One style for each *type* of control on the window -->
<Style TargetType="TextBox">
<Setter Property="Margin" Value="10"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Text="TextBox"/>
<TextBlock Text="TextBlock"/>
</StackPanel>
</Window>
Good luck...
祝你好运...
#2
15
You can link all of your Margin properties by referring to a "Thickness" defined in your resources. I just did this in a project...
您可以通过参考资源中定义的“厚度”来链接所有边距属性。我刚刚在一个项目中做到了这一点......
<!-- somwhere in a resource-->
<Thickness x:Key="CommonMargin" Left="0" Right="14" Top="6" Bottom="0" />
<!-- Inside of a Style -->
<Style TargetType="{x:Type Control}" x:Key="MyStyle">
<Setter Property="Margin" Value="{StaticResource CommonMargin}" />
</Style>
<!-- Then call the style in a control -->
<Button Style="{StaticResource MyStyle}" />
<!-- Or directly on a Control -->
<Button Margin="{StaticResource CommonMargin}" />
The key for me was figuring out that Margin was defined by "Thickness". Let me know if that's clear enough or if you need me to put it in a fully working XAML example.
对我而言,关键在于确定边距是由“厚度”定义的。让我知道这是否足够清楚,或者你是否需要我把它放在一个完全可用的XAML示例中。
#3
1
You can apply margin in your buttons style. And when you use buttons with this style in StackPanel wpf will apply need spacing. for example define in resourcedictionary or whatever:
您可以在按钮样式中应用保证金。当您在StackPanel中使用具有此样式的按钮时,wpf将应用需要间距。例如在resourcedictionary中定义或者其他:
<Style x:Key="myButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10"/>
....
</Style>
then in yor StackPanel xaml definition:
然后在你的StackPanel xaml定义:
<StackPanel>
<Border BorderThickness="0"/>
<Button x:Name="VertBut1" Style="{StaticResource myButtonStyle}" Content="Button1"/>
<Button x:Name="VertBut2" Style="{StaticResource myButtonStyle}" Content="Button2"/>
<Button x:Name="VertBut3" Style="{StaticResource myButtonStyle}" Content="Button3"/>
</StackPanel>
regards Georgi
关于乔治
#1
19
The only solution I can find is to apply the style to each of the controls you are using on the window (I know that's not quite what you want). If you're only using a few different control types it's not too onerous to do something like this:
我能找到的唯一解决方案是将样式应用于您在窗口上使用的每个控件(我知道这不是您想要的)。如果您只使用几种不同的控件类型,那么执行以下操作并不会太繁琐:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- One style for each *type* of control on the window -->
<Style TargetType="TextBox">
<Setter Property="Margin" Value="10"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Text="TextBox"/>
<TextBlock Text="TextBlock"/>
</StackPanel>
</Window>
Good luck...
祝你好运...
#2
15
You can link all of your Margin properties by referring to a "Thickness" defined in your resources. I just did this in a project...
您可以通过参考资源中定义的“厚度”来链接所有边距属性。我刚刚在一个项目中做到了这一点......
<!-- somwhere in a resource-->
<Thickness x:Key="CommonMargin" Left="0" Right="14" Top="6" Bottom="0" />
<!-- Inside of a Style -->
<Style TargetType="{x:Type Control}" x:Key="MyStyle">
<Setter Property="Margin" Value="{StaticResource CommonMargin}" />
</Style>
<!-- Then call the style in a control -->
<Button Style="{StaticResource MyStyle}" />
<!-- Or directly on a Control -->
<Button Margin="{StaticResource CommonMargin}" />
The key for me was figuring out that Margin was defined by "Thickness". Let me know if that's clear enough or if you need me to put it in a fully working XAML example.
对我而言,关键在于确定边距是由“厚度”定义的。让我知道这是否足够清楚,或者你是否需要我把它放在一个完全可用的XAML示例中。
#3
1
You can apply margin in your buttons style. And when you use buttons with this style in StackPanel wpf will apply need spacing. for example define in resourcedictionary or whatever:
您可以在按钮样式中应用保证金。当您在StackPanel中使用具有此样式的按钮时,wpf将应用需要间距。例如在resourcedictionary中定义或者其他:
<Style x:Key="myButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10"/>
....
</Style>
then in yor StackPanel xaml definition:
然后在你的StackPanel xaml定义:
<StackPanel>
<Border BorderThickness="0"/>
<Button x:Name="VertBut1" Style="{StaticResource myButtonStyle}" Content="Button1"/>
<Button x:Name="VertBut2" Style="{StaticResource myButtonStyle}" Content="Button2"/>
<Button x:Name="VertBut3" Style="{StaticResource myButtonStyle}" Content="Button3"/>
</StackPanel>
regards Georgi
关于乔治