简体   繁体   English

在ItemControl中具有依赖项属性的UserControl:如何将ItemSource的元素绑定到UserControl的x:Name?

[英]UserControl with Dependency Property inside an ItemsControl: how to Bind element of ItemSource to x:Name of UserControl?

My class IFrame can contain other IFrames: 我的IFrame类可以包含其他IFrame:

public interface IFrame
{
    int Id { get; }
    string Summary { get; }

    BindableCollection<IFrame> SubFrames { get; set; }
}

To present an IFrame, I have a custom UserControl: 为了展示一个IFrame,我有一个自定义的UserControl:

<UserControl x:Class="Views.FrameView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Views="clr-namespace:Views"
         mc:Ignorable="d" 
         x:Name="FrameXName">
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding ElementName=FrameXName, Path=Id}" Width="50"/>
    </StackPanel>
</UserControl>

With codebehind: 与代码隐藏:

public partial class FrameView : UserControl
{
    public FrameView()
    {
        InitializeComponent();
    }

    public static DependencyProperty IdProperty = DependencyProperty.Register(
        "Id", typeof(int), typeof(FrameView));

    public int Id
    {
        get { return (int) GetValue(IdProperty); }
        set { SetValue(IdProperty, value); }
    }
}

And thus I can display a Frame in a FooView.xaml using: <Views:FrameView x:Name="Frame1" Width="50" Height="50"/> IF I define the following in FooViewModel.cs: public IFrame Frame1 { get { return Frames.ElementAt(1); } } 因此,我可以使用以下命令在FooView.xaml中显示框架: <Views:FrameView x:Name="Frame1" Width="50" Height="50"/> IF我在FooViewModel.cs中定义了以下内容: public IFrame Frame1 { get { return Frames.ElementAt(1); } } public IFrame Frame1 { get { return Frames.ElementAt(1); } }

My Goal: 我的目标:

I want to have a FrameView displayed for every IFrame in a collection, for example: 我想为集合中的每个IFrame显示一个FrameView,例如:

<ItemsControl ItemsSource="{Binding Frames}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Views:FrameView x:Name="{Binding}" Width="50" Height="50"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
</ItemsControl>

Where public BindableCollection<IFrame> Frames is defined. 其中定义了public BindableCollection<IFrame> Frames

Unfortunately, this does not work, the compiler says MarkupExtensions are not allowed for Uid or Name property values, so '{Binding}' is not valid. 不幸的是,这是行不通的,编译器说MarkupExtensions are not allowed for Uid or Name property values, so '{Binding}' is not valid.

How can I achieve my goal? 我如何实现我的目标? Many thanks in advance. 提前谢谢了。

In your datatemplate you can replace 在您的数据模板中,您可以替换

<Views:FrameView x:Name="{Binding}" Width="50" Height="50" />

with

<Views:FrameView Id="{Binding Path=Id}" Width="50" Height="50" />

should work fine 应该工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UserControl中绑定集合依赖项属性 - How to bind collection dependency property in UserControl 如何将依赖属性绑定到 UserControl 中的 DataGridComboBoxColumn? - How to bind dependency property to DataGridComboBoxColumn in UserControl? 在用户控件内部公开控件的itemsource属性 - Expose itemsource property of controls inside a usercontrol 无法绑定用户控件的依赖项属性 - Unable to bind dependency property of usercontrol 如何使用Style将子UserControl的Dependency属性绑定到宿主元素的视图模型的属性? - How to bind a child `UserControl`'s Dependency Property, using `Style`, to a property of the host element's view-model? 如何在另一个UserControl中绑定UserControl? - How to bind UserControl inside another UserControl? WPF UserControl - UserControl DataGrid的SelectedItem,用于将ItemSource绑定到UserControl外部的DataGrid - WPF UserControl - SelectedItem of a Usercontrol DataGrid to bind to a ItemSource to DataGrid outside the UserControl WPF如何绑定用户控件属性 - wpf how to bind usercontrol property 如何验证 UserControl 上的依赖项属性? - How to validate a dependency property on a UserControl? 如何将用户控件属性绑定到列表框 - How to bind a usercontrol property to Listbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM