DataGrid中的SelectedItem从一个UserControl中提供另一个UserControl中的数据 [英] SelectedItem in DataGrid from one UserControl to provide data in another UserControl

查看:73
本文介绍了DataGrid中的SelectedItem从一个UserControl中提供另一个UserControl中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢WPF和数据绑定,但我对如何跨UserControls绑定数据感到困惑。我是WPF的新手,我觉得我对大多数WPF都了解,但我只是不知道如何移动数据。


我的场景:


我有一个带有DataGrid的UserControl,其ItemsSource绑定到ObservableCollection( MyFiles )。我还定义了一个属性来保存SelectedItem( MySelectedFile )。


当我在DataGrid中选择不同的行时,我想要<的内容; g class =" gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace"数据-GR-ID =" 35 QUOT; id =" 35"> MySelectedFile< / g> 在UserControlB中显示


例如......



UserControlA.xaml

< DataGrid ItemsSource =" {Binding路径= MYFILES}" SelectedItem =" {Binding Path = MySelectedFile}" /> 




UserControlA.xaml.cs


 

public ObservableCollection< CustomFileClass> MyFiles {get;组; }

public CustomFileClass MySelectedFile {get;组; }


然后我有一个带有各种CheckBoxes,TextBoxes等的UserControl ......我想要填充UserControlA中来自MySelectedFile的数据。


例如......


UserControlB.xaml


 

< StackPanel>

<! - 我假设每个属性(Text,IsChecked,ItemsSource)都会绑定到某个东西,我只是不确定是什么。 - >

< TextBox x:Name =" FileTitle"文本= QUOT;" />

< CheckBox x:Name =" FileIsActive"器isChecked = QUOT;" />

< ListBox x:Name =" FileTopics"的ItemsSource = QUOT;" />

< / StackPanel>



然后这两个UserControl都显示在MainWindow.xaml中。


 

< StackPanel>

< uc:UserControlA />

< uc:UserControlB />

< / StackPanel>



任何帮助将不胜感激。我觉得一旦掌握了从A到B获取数据的概念,我就会好起来。


谢谢!






$ b $



编辑:我不知道为什么这个论坛会在周围添加g标签MySelectedFile。

解决方案

你还没有真正解释这些属性的位置。


假设他们在viewmodel是mainwindow的datacontext,然后你几乎必须实现inotifyproperychanged。


在MSelectedFile的setter中改变属性(你需要一个propfull)并在你的其他usercontrol中绑定。


此示例可能很有趣:


https://social.technet.microsoft.com/wiki/contents/articles/30564.wpf- uneventful-mvvm.aspx


有一系列厨师和selectedchef看起来像:

 private Person selectedChef; 

public Person SelectedChef
{
get
{
return selectedChef;
}
设置
{
selectedChef = value;
RaisePropertyChanged();
DoSomethingWhenChefChanged();
}
}

你不需要那个dosomethingwhenchefchanged。


你也可以使用/来绑定符号并将所选项目与当前项目同步。


本文通过资源共享集合,因此不是您想要的那样,但它描述了当前项目的事物。


https://social.technet.microsoft.com/wiki/contents/articles/29859.wpf-tips-bind-to-current-item-of-collection.aspx



I really like WPF and the data binding, but I'm getting stuck on how to bind data across UserControls. I'm new to WPF, and I feel like I understand most of WPF, but I'm just not getting how to move data around.

My scenario:

I have a UserControl with a DataGrid whose ItemsSource is bound to an ObservableCollection (MyFiles). There's also a property I've defined to hold the SelectedItem (MySelectedFile).

As I select different rows in the DataGrid, I want the contents of <g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="35" id="35">MySelectedFile</g> displayed in UserControlB.

For example...

UserControlA.xaml

<DataGrid ItemsSource="{Binding Path=MyFiles}" SelectedItem="{Binding Path=MySelectedFile}" />


UserControlA.xaml.cs

public ObservableCollection<CustomFileClass> MyFiles { get; set; }

public CustomFileClass MySelectedFile { get; set; }

And then I have a UserControl with various CheckBoxes, TextBoxes, etc... that I want to populate with the data from MySelectedFile in UserControlA.

For example...

UserControlB.xaml

<StackPanel>

<!-- I'm assuming each attribute (Text, IsChecked, ItemsSource) will be bound to something, I'm just not sure what. -->

<TextBox x:Name="FileTitle" Text="" />

<CheckBox x:Name="FileIsActive" IsChecked="" />

<ListBox x:Name="FileTopics" ItemsSource="" />

</StackPanel>

And then both of these UserControls are displayed in MainWindow.xaml.

<StackPanel>

<uc:UserControlA />

<uc:UserControlB />

</StackPanel>

Any help would be appreciated. I feel like once I grasp this concept of getting data from A to B I'll be good to go.

Thanks!






Edit: I don't know why this forum is adding the g tag around MySelectedFile.

解决方案

You've not really explained where those properties are.

Assuming they're in a viewmodel which is the datacontext of mainwindow then you pretty much just have to implement inotifyproperychanged.

Raise property changed in the setter of MSelectedFile ( you need a propfull ) andbind to that in your other usercontrol.

This sample could be interesting:

https://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx

In there is a collection of chefs and the selectedchef looks like:

private Person selectedChef;
 
public Person SelectedChef
{
    get
    {
        return selectedChef;
    }
    set
    {
        selectedChef = value;
        RaisePropertyChanged();
        DoSomethingWhenChefChanged();
    }
}

You don't need that dosomethingwhenchefchanged.

You could alternatively bind using the / notation and synchronise selected item with current item.

This article shares collections via resources so not what you want in that way but it describes the current item thing.

https://social.technet.microsoft.com/wiki/contents/articles/29859.wpf-tips-bind-to-current-item-of-collection.aspx


这篇关于DataGrid中的SelectedItem从一个UserControl中提供另一个UserControl中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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