我有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>
在运行时,InlaidViewModel
上的ContentControl
绑定是根据应用程序中的其他值设置的。如何在MinHeight
上将MinWidth
和Window
设置为嵌入式控件上的相同值?例如:
<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
的视图模型属性,是否有办法从嵌入式控件继承这些值运行时间?
答案 0 :(得分: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));
}
}