在运行时将窗口的最小宽度和高度设置为嵌入绑定控件中的最小值



我有一个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绑定到这些值,但我不确定我是否要这样做,这更像是一种好奇心。

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

如果我正确理解您的要求,您应该能够绑定到 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));
}
}

最新更新