当子UserControl具有最大宽度为

时间:2015-11-29 11:39:44

标签: c# wpf

很抱歉很长的问题,但我不知道如何缩短它。

我有 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当前设置为Content的类型。 (但总是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的大小。

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

好的,所以我通过提供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;
    }
}

现在它按预期工作