WPF绑定Model的实例对象
创建一个用户控件
1. behindcode指定this.DataContext=XXViewModel
2. "{Binding DataContext.EditModel.MId,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
Xaml代码如下:
<Grid Name="eg" Grid.Row="4" Grid.ColumnSpan="8" Visibility="{Binding DataContext.EditVisible,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Content="年月:" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"></Label>
<Label Content="机种:" Grid.Column="2" Grid.Row="0" VerticalAlignment="Center"></Label>
<Label Content="EMS邮件内容:" Grid.Column="4" Grid.Row="0" VerticalAlignment="Center"></Label>
<DatePicker Background="Blue" Grid.Row="0" Grid.Column="1" SelectedDate="{Binding DataContext.EditModel.Monthly,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"></DatePicker>
<ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" Grid.Row="0" Grid.Column="3"
SelectedValue="{Binding DataContext.EditModel.MId,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" ItemsSource="{Binding DataContext.MachineList,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"/>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步