基于对象名称的对象属性数据绑定到集合项

发布于 2024-10-16 13:28:09 字数 1175 浏览 4 评论 0原文

是否可以实现以下 WPF (Silverlight) 数据绑定方案?

页面上有许多 CustomControl:

<Grid x:Name="grid1">
  ...
  <My:CustCntr x:Name="name1" Property1="{Binding Property1}" />
  <My:CustCntr x:Name="name2" Property1="{Binding Property1}" />
  <My:CustCntr x:Name="name3" Property1="{Binding Property1}" />
  ...
</Grid>

Grid 的 DataContext 是一个 ObservableCollection:

grid1.DataContext = myCollection;
...
ObservableCollection<MyEntity> myCollection= new ObservableCollection<MyEntity>();
...

MyEntity类具有属性 NameProperty1

 MyEntity me1 = new MyEntity { Name = "name1", Property1 = "5" };      
 MyEntity me2 = new MyEntity { Name = "name2", Property1 = "6" };
 MyEntity me3 = new MyEntity { Name = "name3", Property1 = "7" }; 
 ... 
 myCollection.Add(me1); 
 myCollection.Add(me2);
 myCollection.Add(me3); 
 ...    

我可以为每个 CustomControl 中的 Property1 建立数据绑定到 myCollection 的相应项目,其中 CustomControl 的 Name 等于 Name 的值code> 集合项的字段?

Is it possible to implement the following WPF (Silverlight) databinding scenario?

There are a number of CustomControls on a page:

<Grid x:Name="grid1">
  ...
  <My:CustCntr x:Name="name1" Property1="{Binding Property1}" />
  <My:CustCntr x:Name="name2" Property1="{Binding Property1}" />
  <My:CustCntr x:Name="name3" Property1="{Binding Property1}" />
  ...
</Grid>

The Grid's DataContext is an ObservableCollection:

grid1.DataContext = myCollection;
...
ObservableCollection<MyEntity> myCollection= new ObservableCollection<MyEntity>();
...

The MyEntity class has properties Name and Property1.

 MyEntity me1 = new MyEntity { Name = "name1", Property1 = "5" };      
 MyEntity me2 = new MyEntity { Name = "name2", Property1 = "6" };
 MyEntity me3 = new MyEntity { Name = "name3", Property1 = "7" }; 
 ... 
 myCollection.Add(me1); 
 myCollection.Add(me2);
 myCollection.Add(me3); 
 ...    

Can I establish databinding for Property1 in each of my CustomControls to a corresponding item of the myCollection where Name of the CustomControl equals the value of Name field of the collection item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2024-10-23 13:28:09

通常,如果您有一个要在 UI 上显示的 Collection,您要做的就是使用 ItemsControlListBox 等,并将 ItemsSource 设置为 Collection 。示例

<ItemsControl Name="itemsControl1"
              ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <My:CustCntr Property1="{Binding Property1}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

现在,每个 CustCntr 的 DataContext 将是 MyEntity 的一个实例,并且绑定将在 CustCntr.Property1MyEntity 之间设置。 Property1


也就是说,我不确定您当前实现的原因,因此如果您想根据名称创建绑定,我认为您必须求助于

Xaml

<Grid Name="grid1" Loaded="Grid_Loaded">
    <My:CustCntr x:Name="name1" />
    <My:CustCntr x:Name="name2" />
    <My:CustCntr x:Name="name3" />
    <!--...-->
</Grid>

后面的代码< strong>代码隐藏

public ObservableCollection<MyEntity> MyCollection
{
    get;
    private set;
}

更新
每次在代码中修改集合时,请调用此方法 SetBindings。另外,请使用网格的 Loaded 事件来设置首次加载时的所有绑定。

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    SetBindings();
}
private void SetBindings()
{
    foreach (UIElement element in grid1.Children)
    {
        if (element is CustCntr)
        {
            CustCntr custCntr = element as CustCntr;
            foreach (MyEntity myEntity in MyCollection)
            {
                if (custCntr.Name == myEntity.Name)
                {
                    Binding property1Binding = new Binding("Property1");
                    property1Binding.Source = myEntity;
                    property1Binding.Mode = BindingMode.TwoWay;
                    custCntr.SetBinding(CustCntr.Property1Property, property1Binding);
                    break;
                }
            }
        }
    }
}

Normally what you do in a situation where you have a Collection that you want to display on the UI is to use an ItemsControl, ListBox etc. and set the ItemsSource to the Collection. Example

<ItemsControl Name="itemsControl1"
              ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <My:CustCntr Property1="{Binding Property1}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Now the DataContext for each CustCntr will be an instance of MyEntity and the Binding will be set between CustCntr.Property1 and MyEntity.Property1


That said, I'm not sure of the reasons for your current implementation so if you want to create Bindings based on Name I think you'll have to resort to code behind

Xaml

<Grid Name="grid1" Loaded="Grid_Loaded">
    <My:CustCntr x:Name="name1" />
    <My:CustCntr x:Name="name2" />
    <My:CustCntr x:Name="name3" />
    <!--...-->
</Grid>

Code behind

public ObservableCollection<MyEntity> MyCollection
{
    get;
    private set;
}

Update
Call this method, SetBindings, everytime you've modified the collection in code. Also, use the Loaded event for the Grid instead to set all bindings when it's first Loaded.

private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    SetBindings();
}
private void SetBindings()
{
    foreach (UIElement element in grid1.Children)
    {
        if (element is CustCntr)
        {
            CustCntr custCntr = element as CustCntr;
            foreach (MyEntity myEntity in MyCollection)
            {
                if (custCntr.Name == myEntity.Name)
                {
                    Binding property1Binding = new Binding("Property1");
                    property1Binding.Source = myEntity;
                    property1Binding.Mode = BindingMode.TwoWay;
                    custCntr.SetBinding(CustCntr.Property1Property, property1Binding);
                    break;
                }
            }
        }
    }
}
回忆那么伤 2024-10-23 13:28:09

No, that would not be posible.
You could however use an ItemsControl object (there are multple controls that inherit from it), but it might be harder to set the design up then. No for the individual controls, but for the placement of each CustCntr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文