问题描述
我被困在我必须将集合绑定到动态用户控件的部分。情景是这样的。
我有一个动态控件,有一个扩展器,datagrid,组合框和文本框,其中combox和文本框在datagrid内。他们已经有两个集合了。一个与组合框绑定,另一个与数据网格绑定。当项目在combox中更改时,其各自的值将设置为其各自的文本框,依此类推。然后将这对值设置为与datagrid绑定的集合。用户可以添加多个项目。
I am stucked at the part where I have to bind a collection to a dynamic usercontrol. Scenario is something like this.I have a dynamic control, having a expander , datagrid, combobox and textbox, where combox and textbox are inside datagrid. There are already two collections with them. One is binded with combobox and another is binded with datagrid. When the item is changes in combox its respective value is set to its respective textbox, and so on. and this pair of value is then set to the collection binded with datagrid. A user can add multiple items.
现在主要的问题是所有这些都发生在动态添加的用户控件中,即按钮点击事件。用户可以将所需数量的用户控件添加到表单中。
问题是在这个机构。说我已经添加了3个控件。现在在第一个,如果我添加一个代码到集合,那么它也被反映在接下来的两个控件,因为它们与同一个集合绑定。
所以,我想知道是否有任何重新命名/重命名相同的集合,以便不会出现上述情况。
Now the main problem is that all these things are happening inside a user control which is added dynamically, that is on button click event. A user can add desired numbers of user controls to the form.problem is coming in this situtaion. Say I have added 3 controls. Now in 1st one if i add a code to the collection then it gets reflected in the next two controls too, as they are binded with same collection.So, I want to know is there anyway to regenrate/rename the same collection so that the above condition should not arise.
推荐答案
如果没有看到更大的图片,很难回答你的问题,但是我有一种感觉,你会这样做错了。您似乎直接从代码添加用户控件的实例。而不是这样做,您应该在您的XAML中创建某种 ItemsControl
,并在其 ItemTemplate
中为您的用户控制。将 ItemsControl
绑定到视图模型中的集合中,并且仅操纵该集合。
It's hard to answer your question without seeing the bigger picture, however I have a feeling you are going about this the wrong way. It appears that you are adding instances of your user control directly from code. Instead of doing that, you should create some kind of ItemsControl
in your XAML, and in its ItemTemplate
have your user control. Bind that ItemsControl
to a collection in your view model, and only manipulate that collection.
您不应该引用视觉模型中的视觉模型或代码。每当你发现自己直接从代码引用视觉元素,它应该在你的头脑中引发一个警告标志嘿!有一个比这更好的办法!...
You should not be referring to visual controls in your view model or code behind. Whenever you find yourself referencing visual elements directly from code, it should raise a warning flag in your mind "Hey! There's a better way than that!"...
示例:
视图模型:
public class ViewModel
{
public ObservableCollection<MyDataObject> MyDataObjects { get; set; }
public ViewModel()
{
MyDataObjects = new ObservableCollection<MyDataObject>
{
new MyDataObject { Name="Name1", Value="Value1" },
new MyDataObject { Name="Name2", Value="Value2" }
};
}
}
public class MyDataObject
{
public string Name { get; set; }
public string Value { get; set; }
}
包含列表框和数据模板的窗口XAML片段: p>
The window XAML fragment containing the list box and the data template:
<Window.Resources>
...
<DataTemplate x:Key="MyDataTemplate">
<local:MyUserControl/>
</DataTemplate>
</Window.Resources>
...
<ListBox ItemsSource="{Binding MyDataObjects}"
ItemTemplate="{StaticResource MyDataTemplate}"
HorizontalContentAlignment="Stretch"/>
用户控件:
<UniformGrid Rows="1">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Value}" HorizontalAlignment="Right"/>
</UniformGrid>
这篇关于在运行时如何将可观察的集合绑定到多个用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!