【问题标题】:Window SizeToContent issue when child UserControl has grid with Column of max Width当子用户控件的网格具有最大宽度的列时,窗口大小到内容问题
【发布时间】:2015-11-29 11:39:43
【问题描述】:

对不起,问题的标题很长,但我不知道如何缩短它。

我有 SettingsView 带有内容控制的窗口:

<Window x:Class="IsoMan.UI.Settings.Views.SettingsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SettingsView" WindowStartupLocation="CenterOwner"
    SizeToContent="WidthAndHeight">
    <ContentControl Name="ContentControl"/>
</Window>

在代码隐藏中,我正在设置 ContentControl 的内容。 这里我简化了很多,通常它是通过绑定动态设置的,所以我现在从来没有现在将什么类型的 UserControl 设置为内容。 (但始终是 UserControl)

public partial class SettingsView : Window
{
    public SettingsView()
    {
        InitializeComponent();   
        ContentControl.Content = new UserSettingsView();
    }
}

这是我的 UserSettingsView.xaml,它是 SettingsView 的子控件:

<UserControl x:Class="IsoMan.UI.Settings.UserSettings.Views.UserSettingsView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        MinHeight="400" MinWidth="700">
    <DataGrid ItemsSource="{Binding UsersList}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding FirstName}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
</UserControl>

这是我的问题。

我将 Window SizeToContent 设置为 WidthAndHeight,因为我希望它的大小可以调整为子控件大小(在本例中为 UserSettingsView)。

UserSettingsView 设置了 MinWidth/MinHeight,因为我希望用户无法将其缩小(按钮/网格演示)。该控件具有一列宽度为“*”的网格。我希望它占用所有可用空间。

现在会发生什么:

显示 SettingsView 后,它的宽度是我屏幕的最大宽度。我认为这是因为 UserSettingsView 中网格的列宽设置为“*”。

我想要达到的目标:

显示 SettingsView 后,它的宽度应该是 UserSettingsView 的 MinWidth。此外,当用户尝试调整 SettingsView 窗口大小时,子控件 UserSettingsView 也应调整大小。

【问题讨论】:

  • 我只是好奇,DataGrid 是否只有一列?
  • 不,它有很多列。其中一些具有固定宽度,另一些宽度设置为“*”。

标签: c# wpf


【解决方案1】:

在设置内容时绑定宿主窗口的 MinWidth 和 MinHeight 属性。在窗口 xaml 中删除 SizeToContent="WidthAndHeight",而不是在子 ContentControl 上执行 HorizontalAlignment="Stretch", VerticalAlignment="Stretch" 并在设置实际内容时在后面的代码中使用:

public partial class SettingsView : Window
{
    public SettingsView()
    {
        InitializeComponent();
        var _content = new UserSettingsView();
        ContentControl.Content = _content;
        this.MinWidth = _content.MinWidth;
        this.MinHeight=_content.MinHeight;
        this.Width=this.MinWidth;
        this.Height=this.MinHeight;
    }
}

【讨论】:

  • 不可能按照你的建议去做,因为我的 ContentControl 的内容绑定到一些可观察的属性,正如我在问题中描述的那样。我不知道内容何时更改以及更改为什么。
  • 您确实有权访问主机窗口类,不是吗?观察内容属性更改,将新值转换为 UserControl。之后使用上面的代码。
【解决方案2】:

好的,所以我通过向 ContentControl 提供在内容更改时发出通知的能力来解决我的问题。

ContentControlWithNotification.cs:

public class ContentControlWithNotification : ContentControl
{
    static ContentControlWithNotification()
    {
        ContentProperty.OverrideMetadata(typeof(ContentControlWithNotification), new FrameworkPropertyMetadata(OnContentChanged));
    }

    private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var mcc = d as ContentControlWithNotification;

        if (mcc.ContentChanged != null)
        {
            var args = new DependencyPropertyChangedEventArgs(ContentProperty, e.OldValue, e.NewValue);
            mcc.ContentChanged(mcc, args);
        }
    }

    public event DependencyPropertyChangedEventHandler ContentChanged;
}

然后我将事件处理程序附加到 XAML 中的 ContentChanged 事件:

<l:ContentControlWithNotification x:Name="ActiveItem" ContentChanged="ActiveItem_OnContentChanged"/>

这是我的处理程序实现:

public partial class SettingsView : Window
{
    public SettingsView()
    {
        InitializeComponent();
    }

    private void ActiveItem_OnContentChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var newUserControl = (UserControl)e.NewValue;
        MinWidth = newUserControl.MinWidth;
        MinHeight = newUserControl.MinHeight;
        Width = MinWidth;
        Height = MinHeight;
    }
}

现在它可以正常工作了

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多