0

我们正在努力遵循这些准则。为此,我想设置应用程序级别的样式或属性来设置控件之间的边距。

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

我可以通过在 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">

这是正确的方法还是我们通过更简单的方法实现相同的方法。

4

1 回答 1

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}">
于 2011-07-08T13:19:50.407 回答