- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我的 WPF UserControl 知识只有一个小时了。因此,如果有很多关于这个问题的教程或/和答案,请原谅我(老实说,我认为这无法完成,需要重新编写代码……因此我想问的原因)
因此,在创建 UserControl 之前,我有一个数据网格,它根据用户在文本框中键入的文本筛选客户。一旦找到,该筛选器 DataGrid 的 SelectedItem 将用于绑定(bind)到包含新集合的新 DataGrid。
所以……
过滤数据网格 XAML
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource cvsCustomers}}"
一旦用户在该网格中选择了一个客户,
新的 DataGrid 将包含基于 SelectedCustomer 的属性行
ItemsSource="{Binding SelectedCustomer.CustomerOrders}"
一切都很好,一切正常。
但是,我将在我的项目中大量使用此筛选客户结果功能,因此我创建了一个 UserControl,其中筛选器 DataGrid 正在运行。
我已将此 UserControl 放在 View 中,所以问题是我需要将 Usercontrol 中的任何 selectedItem 绑定(bind)到 View 中的 DataGrid。 (如上)
所以我在 View 的 DataGrid 中需要这样的东西。
ItemsSource="{Binding ElementName=myUserControl, Path=SelectedCustomer.CustomerOrders}"
好吧有点啰嗦,但我希望你能理解这个问题,而且我已经提供了足够的关于手头主题的知识。如果我做错了什么,请告诉我并否决这个问题。
干杯
最佳答案
您可以将新的依赖属性添加到您的自定义用户控件并将您的数据网格项目源绑定(bind)到该属性。确保处理用户控件数据网格上的选择更改事件,并将依赖属性设置为所选项目。
public object MySelectedItem
{
get { return (object)GetValue(MySelectedItemProperty); }
set { SetValue(MySelectedItemProperty, value); }
}
// Using a DependencyProperty as the backing store for MySelectedItem. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MySelectedItemProperty =
DependencyProperty.Register("MySelectedItem", typeof(object), typeof(YOURUSERCONTROLTYPE), new UIPropertyMetadata(null));
处理选择改变事件
public YourUserControl()
{
InitializeComponent();
dgv.SelectionChanged += dgv_SelectionChanged;
}
void dgv_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MySelectedItem = dgv.SelectedItem;
}
然后绑定(bind)到
ItemsSource="{Binding ElementName=myUserControl, Path=MySelectedItem.CustomerOrders}"
关于c# - WPF UserControl - Usercontrol DataGrid 的 SelectedItem 绑定(bind)到 UserControl 外的 DataGrid 的 ItemSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23381560/
我正在尝试将我的 ViewModel (ComboBox) 中的 SelectedItems (ListView) 和 SelectedItem 或 SelectedCategory 多重绑定(bin
一两天以来,我一直试图找出一个奇怪的 WPF 错误。我有一个 ListBox 控件,它绑定(bind)到 PolicyId 的 ObservableCollection,它是一个实现 INotifyP
我有一个 ASP DropDown,声明如下: 数据是这样填的: DropDown1.DataSource = source; DropDown1.DataTextField = "Key"; D
我有一个绑定(bind)到 ListCollectionView 的 ListBox,有一次我在列表框中选择了一个项目,然后在未来我重新创建了 ListCollectionView,但是 ListBo
我有一个简单的组合框,其中复选框作为项目。如何防止项目的实际选择。用户应该只能选中或取消选中复选框? 目前,如果我单击一个元素(而不是内容或检查本身),它将被选中。这样做的问题是:ComboBox 的
有没有办法知道在 Windows 资源管理器中选择了哪个文件?我一直在看这里发布的教程 Idiots guide to ...但描述的行动是: 徘徊 语境 菜单属性 拖 拖放 我想知道是否有在选择文件
我有一个 WPF 数据网格。 DataGrid 绑定(bind)到 IList。该列表有许多项目,因此 DataGrid MaxHeight 设置为预定义值,并且 DataGrid 自动显示滚动条。选
好的,已经使用 WPF 有一段时间了,但我需要一些帮助。 我有一个 ComboBox如下所示: 每当我离开选项卡 1 然后回到它时,选择就会被删除。
示例代码如下: ddlCat.Items.Insert(0, new ListItem("Item1", "1")); ddlCat.Items.Insert(1, new ListItem("It
我想使用 SelectedItem 将选择设置为代码中的组合框。 我只能通过使用 SelectedValue 让它工作。 SelectedItem 将在堆栈跟踪的顶部抛出一个空引用异常: 在 Atta
这是我在这个很棒的平台上的第一个问题,是的,我在这里搜索了如何执行此操作,在我的情况下很少见,因为它应该可以工作,但我从 SelectedItem 中得到的只是一个字符串(它的内容,这是一个 Text
我有一个ListBox,其中包含填充TextBoxes 的项目。当从 ListBox 中进行选择时,如何识别选择的文本字符串。这是我的 ListBox XAML 代码:
我有一个列表框,其中填充了来自 ImageDomainService(RIA 服务)的列表。我想从 ListBox 中选择一张图像并在其旁边显示该图像的较大版本。图像单独存储在/images/文件夹中
我有一个绑定(bind)到 ObservableCollection 的 ListView ItemsSource。我通过 MVVM 添加了一个属性来跟踪 ListView.SelectedItem。
我正在尝试从 QTableView 小部件(下面复制的片段)返回选定行的向量,但是返回的值与选择不对应,我相信我不了解 QModelIndexList/QModelIndex 与 QTableView
我试图寻找答案,但我没有任何运气。基本上我有一个 ListView ,它绑定(bind)到从 View 模型返回的集合。我将 ListView 的选定项目绑定(bind)到我的 ListView 中的
我有一个实现 INotifyPropertyChanged 的 View 模型.在这个 viewModel 上有一个名为 SubGroupingView 的属性。 .此属性绑定(bind)到组合框的选
我有一个 ComboBox,它的 ItemsSource 绑定(bind)到静态 List的选项。 ComboBox 是绑定(bind)到 CustomObject 类的表单的一部分,该类的属性之一是
我有一个将 SelectedItem 绑定(bind)到 ViewModel 的 ComboBox。 当用户在 View ComboBox 中选择一个新项目时,我想显示一个提示并验证他们是否要进行更
我有一个 ViewModel(其结构的伪代码): class ViewModel { public List Packages { get; set; } } enum Type {
我是一名优秀的程序员,十分优秀!