【问题标题】:WPF bind DataContext in xaml to ViewModel in codeWPF 将 xaml 中的 DataContext 绑定到代码中的 ViewModel
【发布时间】:2009-10-24 07:25:10
【问题描述】:

我的应用在 XAML 中的简化版本:

<UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:uc="clr-namespace:myApp.MyControls"    
      x:Class="myApp.View.MyItem"
      x:Name="testWind"
      Width="Auto" Height="Auto" Background="White">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <GroupBox x:Name="GroupBox" Header="G" Grid.Row="0">
            <WrapPanel>
                <GroupBox x:Name="GroupBox11">
                    <StackPanel Orientation="Vertical">
                        <uc:customControl1 Margin="0,4,0,0" 
                                           property1="{Binding prop1.property1}" 
                                           property2="{Binding prop1.property2}"
                                           property3="{Binding prop1.property3}"/>
                        <uc:customControl2 Margin="0,4,0,0" 
                                           property1="{Binding prop2.property1}" 
                                           property2="{Binding prop2.property2}"
                                           property3="{Binding prop2.property3}"/>
                    </StackPanel>
                </GroupBox>
            </WrapPanel>
        </GroupBox>
    </Grid>
</UserControl>

在 C# 中我有这个:

namespace MyApp.ViewModel
{
    public class MyItemViewModel
    {
        public object prop1 { get; set; }
        public object prop2 { get; set; }
    }
}

在 MyItem.cs 我这样做:

namespace MyApp.View
{
    public partial class MyItem : UserControl
    {
        public MyItem()
        {
             this.InitializeComponent();
             MyItemViewModel vm = new MyItemViewModel();
             vm.prop1 = new blah blah(); // setting properties
             vm.prop2 = new blah blah(); // setting properties
             this.DataContext = vm;
        }
    }
}

当您最终拥有太多控件时,这可能会变得难以维护。那么,我如何告诉 XAML 执行以下操作:

    <uc:customControl1 Margin="0,4,0,0" DataContext="prop1"/>
    <uc:customControl2 Margin="0,4,0,0" DataContext="prop2"/>

【问题讨论】:

    标签: c# wpf xaml data-binding datacontext


    【解决方案1】:

    你可以给每个控件一个唯一的名字

    <uc:customControl1 x:Name="mycontrol1" Margin="0,4,0,0" DataContext="prop1"/>
    <uc:customControl2 x:Name="mycontrol2" Margin="0,4,0,0" DataContext="prop2"/>
    

    然后在您的代码中,您可以更改数据上下文

       mycontrol1.DataContext = vm1;
       mycontrol2.DataContext = vm2;
    

    【讨论】:

    • 嗨,我试过了,但它似乎不起作用。除了名称之外,XAML 中的 DataContext="prop1" 与 C# 中的 mycontrol1.DataContext=vm1 有何关系?为什么在 XAML 中设置“prop1”名称,而在 C# 中设置 vm1?
    • @vark ,我只是按照你的例子。 vm1 和 vm2 代表不同的数据上下文。
    • 嘿,安德鲁,是的,我明白这一点。但是我无法完成这项工作。我在这里建立了一个小示例项目来说明这两种方法:2shared.com/file/8688933/866fc885/usercustom.html。你的解释中我遗漏了什么吗?
    猜你喜欢
    • 2015-10-22
    • 2012-10-14
    • 2012-07-15
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2023-03-31
    相关资源
    最近更新 更多