C# 努力将主窗口数据上下文传递到弹出窗口 模型
其中SpecialEvent为:C# 努力将主窗口数据上下文传递到弹出窗口 模型,c#,wpf,xaml,C#,Wpf,Xaml,其中SpecialEvent为: SpecialEvent Description Location Type 模型视图 据我所知,这是对MVVM的正确使用。但现在我想更进一步。我有这个图像,我将用于上下文菜单: <Image Grid.Column="1" HorizontalAlignment="Right" Source="Images\event_time.png" Margin="2"> <Image.ContextMenu>
SpecialEvent
Description
Location
Type
模型视图
据我所知,这是对MVVM的正确使用。但现在我想更进一步。我有这个图像
,我将用于上下文菜单
:
<Image Grid.Column="1" HorizontalAlignment="Right" Source="Images\event_time.png" Margin="2">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Set Special Event" Command="{Binding SetSpecialEventCommand, Mode=OneWay}">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Meeting.IsSpecialEvent}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
<MenuItem Header="Remove Special Event">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Meeting.IsSpecialEvent}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
</Image.ContextMenu>
</Image>
问题是。。。我们有当前在组合框中选择的会议对象
如果我右键单击并选择设置特殊事件,我想显示一个弹出窗口。这就是我有点困惑的地方。我知道我必须在弹出窗口中使用“UpdateSourceTrigger=Explicit”,然后在OK按钮中执行更新(特殊事件描述、位置、类型)
我的弹出窗口有问题。Xaml:
<Window x:Class="OCLMEditor.SpecialEventWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OCLMEditor"
mc:Ignorable="d"
Title="SpecialEventWindow" Height="300" Width="300">
<Grid>
<TextBox x:Name="textDescription" HorizontalAlignment="Left" Height="23" Margin="38,49,0,0" TextWrapping="Wrap"/>
</Grid>
</Window>
_SetSpecialEventCommand = new DelegateCommand<string>(
(s) =>
{
SpecialEventWindow windowEvent = new SpecialEventWindow();
windowEvent.DataContext = _Meeting.SpecialEvent;
windowEvent.ShowDialog();
},
(s) => true);
_SetSpecialEventCommand=新的DelegateCommand(
(s) =>
{
SpecialEventWindowEvent=新建SpecialEventWindow();
windowEvent.DataContext=\u Meeting.SpecialEvent;
windowEvent.ShowDialog();
},
(s) =>正确);
然而,在弹出窗口的XAML编辑器中,它认为它没有DataContext。因此,它不允许我将文本框绑定到其中一个属性
更新
我不确定提出的评论是什么,因为他们显然错过了我设置DataContext的代码。但是很容易错过。就在那里。:)
\u SetSpecialEventCommand=新建DelegateCommand(
(s) =>
{
SpecialEventWindowEvent=新建SpecialEventWindow();
windowEvent.DataContext=\u Meeting.SpecialEvent;
windowEvent.ShowDialog();
},
(s) =>正确);
它确实有效。但我的意思是,当我在代码中设置datacontext时,XAML编辑器似乎并不知道这一点。所以当我点击控件并进入绑定。。。它没有出现
我的主窗口有:
因此,它显示了活动数据上下文,我可以使用IDE在绑定中导航。但不是在这个弹出窗口上。我必须在代码编辑器中手动完成所有操作。您在运行时为弹出窗口提供了一个
DataContext
,但不是在设计时。IDE不够聪明,无法通过分析代码来确定您希望它做什么
您的主窗口在设计时具有DataContext的原因是,您在XAML中以声明方式提供了一个DataContext,这不需要对IDE进行深入了解,就可以了解您的需求;您只是在设计时设置了一个属性:
<Window.DataContext>
<local:OCLMEditorViewModel />
</Window.DataContext>
这需要specialvent
具有默认构造函数。它可以有特殊的“模拟数据”初始化时,它(或不)
更简单(但没有那么好),在您的情况下,您可以像在主窗口中那样实例化它:
<Window.DataContext>
<local:SpecialEvent />
</Window.DataContext>
这将在设计时为您提供一个键入的
DataContext
。在运行时,显示对话框的命令将用它从\u Meeting.SpecialEvent
获得的特定实例替换该垃圾实例,因为所有XAML工作都在窗口的构造函数完成时完成 @EdPlunkett对不起,但是代码有:windowEvent.DataContext=\u Meeting.specialvent代码>。我正要编辑我的问题。你是说你希望XAML编辑器在编辑命令显示的窗口时执行该命令吗?在设计时,你没有给弹出窗口一个DataContext
。仅在运行时。您可以给它一个窗口。DataContext
元素,就像主窗口一样:
。这将在设计时以及在运行时创建时为其提供该类型的DataContext。但在运行时,该命令中的代码将用同一类的不同实例替换该DataContext。
public Data.MeetingInfo.Meeting Meeting
{
get { return _Meeting; }
set
{
// Has the existing meeting object changed at all?
if(_Meeting != null && _Meeting.IsDirty)
{
// Yes, so save it
_Model.Serialize();
_Meeting.MarkClean();
}
// Now we can update to new value
if (value != null)
{
_Meeting = value;
OnPropertyChanged("Meeting");
}
}
}
private Data.MeetingInfo.Meeting _Meeting;
<Image Grid.Column="1" HorizontalAlignment="Right" Source="Images\event_time.png" Margin="2">
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Set Special Event" Command="{Binding SetSpecialEventCommand, Mode=OneWay}">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Meeting.IsSpecialEvent}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
<MenuItem Header="Remove Special Event">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Meeting.IsSpecialEvent}" Value="True">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
</Image.ContextMenu>
</Image>
<Window x:Class="OCLMEditor.SpecialEventWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OCLMEditor"
mc:Ignorable="d"
Title="SpecialEventWindow" Height="300" Width="300">
<Grid>
<TextBox x:Name="textDescription" HorizontalAlignment="Left" Height="23" Margin="38,49,0,0" TextWrapping="Wrap"/>
</Grid>
</Window>
_SetSpecialEventCommand = new DelegateCommand<string>(
(s) =>
{
SpecialEventWindow windowEvent = new SpecialEventWindow();
windowEvent.DataContext = _Meeting.SpecialEvent;
windowEvent.ShowDialog();
},
(s) => true);
_SetSpecialEventCommand = new DelegateCommand<string>(
(s) =>
{
SpecialEventWindow windowEvent = new SpecialEventWindow();
windowEvent.DataContext = _Meeting.SpecialEvent;
windowEvent.ShowDialog();
},
(s) => true);
<Window.DataContext>
<local:OCLMEditorViewModel />
</Window.DataContext>
<Window
...
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=local:SpecialEvent, IsDesignTimeCreatable=True}"
...
>
<Window.DataContext>
<local:SpecialEvent />
</Window.DataContext>