【问题标题】:Set minimum width and height of a window to the minimum values from the embedded bound control at run time在运行时将窗口的最小宽度和高度设置为嵌入式绑定控件的最小值
【发布时间】:2016-12-15 14:12:16
【问题描述】:

我有一个Window

<Window x:Class="ClientApp.Views.ModalWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="332" Width="536" >

    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">
        <ContentControl Content="{Binding Path=InlaidViewModel}" Margin="0" 
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </Grid>
</Window>

在运行时,ContentControl 上的 InlaidViewModel 绑定是根据应用程序中的其他值设置的。如何在绑定时将Window 上的MinHeightMinWidth 设置为嵌入式控件上的相同值?例如:

<Window x:Class="Roryap.BillCalendar.ClientApp.Views.ModalWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="332" Width="536" 
        MinHeight="{Binding **what goes here**}" MinWidth="{Binding **what goes here**}">

我知道我可以为 Window 的底层视图模型添加属性以绑定到这些值,但我不确定我是否想这样做,这更像是一种好奇心。

问题是:如果我不想为我的窗口拥有MinHeightMinWidth 的视图模型属性,有没有办法从运行时绑定的嵌入式控件继承这些值?

【问题讨论】:

  • UserControl 可以遍历视觉树直到Window,这是有道理的,因为嵌入式控件定义了大小。或者您可以简单地使用自动调整窗口(请参阅SizeToContent),不确定它是否适用于您的情况。
  • 如果嵌入式控件是同一个 DOM 的一部分并且同一个 measure()/arrange() 传递,那么你可以通过 MinHeight="{Binding ActualHeight, ElementName=EmbeddedControlName}" 来引用它的渲染大小,如果我理解的话问题正确。
  • @ChrisW。 ——我喜欢它的发展方向。我对 XAML 相当缺乏经验;您能否详细说明该控件是同一个 DOM 和测量/排列通道的一部分?什么时候调用的?
  • @ChrisW。 ——对不起,菜鸟评论^。我现在正在阅读它。这是一个好帖子:csharpramblings.blogspot.com/2012/05/…
  • 仍然,我无法找到有关如何根据运行时绑定结果的嵌入式控件安排容器布局的信息。

标签: c# .net wpf xaml mvvm


【解决方案1】:

如果我正确理解您的要求,您应该能够像这样绑定到 ControlControl 内容的 MinHeight 和 MinWidth 属性:

<Window x:Class="ClientApp.Views.ModalWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" Height="332" Width="536"
    MinHeight="{Binding Path=Content.MinHeight, ElementName=cc}"
    MinWidth="{Binding Path=Content.MinWidth, ElementName=cc}">

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">
    <ContentControl x:Name="cc" Content="{Binding Path=InlaidViewModel}" Margin="0" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>

这假定 InlaidViewModel 属性返回的任何对象都具有 System.Double 类型的 MinHeight 和 MinWidth 属性:

public partial class ModalWindow : Window
{
    private readonly WindowViewModel _viewModel;
    public ModalWindow()
    {
        InitializeComponent();
        _viewModel = new WindowViewModel();
        DataContext = _viewModel;
        Loaded += async (s, e) =>
        {
            _viewModel.InlaidViewModel = new InlaidViewModel();
            //wait 2 seconds before setting the MinHeight property
            await Task.Delay(2000);
            _viewModel.InlaidViewModel.MinHeight = 500;
        };
    }
}

public class WindowViewModel : INotifyPropertyChanged
{
    private InlaidViewModel _inlaidViewModel;
    public InlaidViewModel InlaidViewModel
    {
        get { return  _inlaidViewModel; }
        set {  _inlaidViewModel = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

public class InlaidViewModel : INotifyPropertyChanged
{
    private double _minHeight;
    public double MinHeight
    {
        get { return _minHeight; }
        set { _minHeight = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

【讨论】:

  • 我可以欣赏这种方法,因为它正是我在 cmets 中展示的,但 Content.* 的预期回报是多少?这将如何是一个有效的属性值来绑定到 ActualHeight/ActualWidth 是控件的呈现大小并提供实际值的位置?我问是因为我从未将 Content.* 视为任何相关事物的附加属性,如果存在新事物,我不介意学习吗?
  • 如前所述,Content 的预期返回将是一个具有 MinHeight 和 MinWidth 属性的对象,即“InlaidViewModel”对象。我已经更新了我的答案以澄清。
  • @mm8 -- 谢谢。这个想法是我想完全在 XAML 中设置窗口和控件的尺寸,而不必涉及视图上的属性。我认为 Chris W. 在我的问题下的评论已经到了那里,但我仍然无法让它发挥作用。
  • 您的意思是“涉及视图模型的属性”吗?如果要将窗口的 MinHeight 属性绑定到 ContentControl 本身的 ActualHeight 属性,只需将绑定路径更改为“ActualHeight”:MinHeight="{Binding Path=ActualHeight, ElementName=cc}"。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-18
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2015-03-31
相关资源
最近更新 更多