从usercontrol发送(或绑定参数)到WPF MVVM中的另一个用户控件 [英] Send (or bind parameters) from a usercontrol to another usercontrol in WPF MVVM

查看:70
本文介绍了从usercontrol发送(或绑定参数)到WPF MVVM中的另一个用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们用户MVVM模型,有一个UserControl,它有一个ItemControl,在ItemTemlate中有一个usercontrol(usercontrol2),如下所示:

We user MVVM Model, There is a UserControl that has a ItemControl which in ItemTemlate a usercontrol (usercontrol2) is located, like this:

<UserControl x:Class="usercontrol1">

...

< ItemsControl ItemsSource =" {Binding GroupsList}" ">

<ItemsControl ItemsSource="{Binding GroupsList}" ">

...

< ItemsControl.ItemTemplate>&NBSP;

<ItemsControl.ItemTemplate> 

<的DataTemplate>&NBSP;

<DataTemplate> 

< span style ="white-space:pre"> < local:usercontrol2  /> 

<local:usercontrol2  /> 

< / DataTemplate> 

</DataTemplate> 

< /ItemsControl.ItemTemplate> 

</ItemsControl.ItemTemplate> 

< / ItemsControl>

</ItemsControl>

< / UserControl>

</UserControl>

我们将发送(或使用绑定)参数GroupNumber(GroupList的属性) 从  usercontrol1到usercontrol2,怎么做?

we are going to to sent (or using binding) parameter GroupNumber ( a propery of GroupList)  from usercontrol1 to usercontrol2,How can do that?

推荐答案

默认情况下, UserControl DataContext 是从其父
除非您指定其他数据源。

In default, the DataContext of UserControl is inherited from its parent unless you specify another data source.

所以 如果
GroupNumber 是
GroupsList的项目,您可以直接绑定它。

So if the GroupNumber  is a property of GroupsList's Item, you can bind it directly.

我做了一个样本供你参考。

I made a sample for your reference.

MainWindow:

MainWindow:

<Window x:Class="wpfAppDemo.wpfTest.MainWindow"
        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:wpfAppDemo.wpfTest"
        mc:Ignorable="d"
       x:Name="mainwindow"
        Title="MainWindow" Height="300" Width="800">
    <StackPanel x:Name="rootGrid" DataContext="{Binding ElementName=mainwindow}">      
        <local:UserControl1/>
    </StackPanel>
</Window>

In 代码背后,我有一个GroupItem列表,它有两个名为  GroupName和GroupNumber的属性。

In  Code behind , I have a list of GroupItem which have two properties named  GroupName and GroupNumber.

namespace wpfAppDemo.wpfTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<GroupItem> GroupList { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            GroupList = new ObservableCollection<GroupItem> {
                new GroupItem { GroupName="Bob", GroupNumber=7 },
                new GroupItem { GroupName ="Emily", GroupNumber=7 },
                new GroupItem { GroupName ="James", GroupNumber=8 },};
        }
    }

    public class GroupItem
    {
        public string GroupName { get; set; }

        public int GroupNumber { get; set; }
    }
}




$

UserControl1:

UserControl1:

<UserControl
    x:Class="wpfAppDemo.wpfTest.UserControl1"
    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:local="clr-namespace:wpfAppDemo.wpfTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="300"
    d:DesignWidth="300"
    mc:Ignorable="d">
    <StackPanel>
        <TextBlock Text="this is user control 1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>
        <ItemsControl  
            Background="#E4CECE"
            ItemsSource="{Binding Path=GroupList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type GroupItem}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            FontSize="20"
                            Margin="0,0,5,0"
                            Text="{Binding GroupName}" />
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            FontSize="20"
                            Margin="0,0,10,0"
                            Text=":" />
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            FontSize="20"
                            Margin="0,0,10,0"
                            Text="{Binding GroupNumber}" />
                        <local:UserControl2 />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

在UserControl2中:

In UserControl2:

<UserControl
    x:Class="wpfAppDemo.wpfTest.UserControl2"
    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:local="clr-namespace:wpfAppDemo.wpfTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="100"
    d:DesignWidth="300"
    x:Name="uc1"
    Background="Transparent"
    mc:Ignorable="d">
    <Border
        Padding="0"
        Background="#C69A9A"
        BorderBrush="Black"
        BorderThickness="1"
        CornerRadius="8">
        <StackPanel
            Margin="10"
            AllowDrop="True"
            Background="AntiqueWhite" >    
           <TextBlock Text="this is user control 2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>          
            <TextBlock Text="{Binding GroupNumber}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20"/>
        </StackPanel>
    </Border>
</UserControl>




此致,

鲍勃



Bob


这篇关于从usercontrol发送(或绑定参数)到WPF MVVM中的另一个用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆