设置应用程序级别边距属性

时间:2011-07-08 13:08:39

标签: wpf c#-4.0 margins

我们正在尝试遵循these指南。 为此,我想设置应用程序级别样式或属性以设置控件之间的边距。

我无法通过样式设置边距,因为它要求我提供目标对象,并且可能存在我不想遵循上述情况的情况。

我可以通过在App.xaml.cs中创建一些getter属性来设置边距。

   /// <summary>
    /// Gets the margin to be set all around the dialog
    /// </summary>
    public Thickness MarginsAllAroundDialog
    {
      get
      {
        // returns default margin
        return new Thickness(7);
      }
    }

并将对话框的边距设置为:

<Window x:Class="XXX.Views.MainWindow"
        x:Name="mainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://www.codeplex.com/prism"
        Title="MainWindow" 
        Margin="{Binding Path=MarginsAllAroundDialog, Source={x:Static Application.Current}}"
        Height="350" 
        Width="525"
        WindowState="Maximized">

这是正确的方法,还是通过简单的手段实现同样的目标。

1 个答案:

答案 0 :(得分:3)

我认为你的方法已经足够好了,但考虑在XAML中声明边距。

您可以在App.xaml

中将边距定义为资源
<Application>
    <Application.Resources>
        <Thickness x:Key="MarginsAllAroundDialog" Bottom="7" Left="7" Right="7" Top="7" />
    </Application.Resources>
</Application>

引用这些资源而不是使用绑定:

<Window x:Class="XXX.Views.MainWindow"
        Margin="{StaticResource MarginsAllAroundDialog}">
相关问题