简体   繁体   English

无法将 ObservableCollection 绑定到包含自定义控件的 listView

[英]Cannot bind an ObservableCollection to a listView containing custom controls

I'm trying to make an app for my own use and need to use a custom control as Items for a ListView.我正在尝试制作一个供我自己使用的应用程序,并且需要使用自定义控件作为 ListView 的项目。 The control contains an image and a number of copies to be printed of that image.该控件包含一个图像和该图像要打印的副本数。

I can have the control to display both the pic and the number of copies if I bind it from code as I will show, but in that case, I cannot get the number of copies to be updadted in the Items of the ListView.如果我从代码中绑定它,我可以控制显示图片和副本数量,如我将显示的那样,但在这种情况下,我无法在 ListView 的项目中获取要更新的副本数量。 I've tried to Bind it from XAML but I cannot make it work.我试图从 XAML 绑定它,但我无法让它工作。

This is the relevant code of the control:这是控件的相关代码:

public partial class SelectorImpresiones : UserControl
{
    private uint m_NumeroDeCopias;
    private string fichero;

    public static readonly DependencyProperty FicheroOrigenProperty =
        DependencyProperty.Register
        (
            @"FicheroOrigen",
            typeof(string),
            typeof(SelectorImpresiones),
            new FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
            );

    public static readonly DependencyProperty NumeroDeCopiasProperty =
        DependencyProperty.Register
        (
            @"NumeroDeCopias",
            typeof(uint),
            typeof(SelectorImpresiones),
            new FrameworkPropertyMetadata(default(uint), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
                );

    public uint NumeroDeCopias
    {
        get { return (uint)GetValue(NumeroDeCopiasProperty); }
        set { SetValue(NumeroDeCopiasProperty, value); }
    }

    public string FicheroOrigen
    {
        get { return GetValue(FicheroOrigenProperty) as string; }
        set { SetValue(FicheroOrigenProperty, value); }
    }

The values and properties get updated correctly within the control when the number of copies to be printed are modified.修改要打印的份数时,控件中的值和属性会得到正确更新。

This is the structure contained in the ObervableCollection that I'm trying to bind这是我试图绑定的 ObervableCollection 中包含的结构

public struct ImagenPedido
{
    public int IDFoto { get; set; }
    public uint NumeroCopias { get; set; }
    public string RutaFichero { get; set; }
}

And this is how I'm trying to use the control in the ListView:这就是我尝试在 ListView 中使用控件的方式:

    <ListView x:Name="lvListaImagenes" ItemsSource="{Binding ListaImagenPedido}" HorizontalAlignment="Stretch" Margin="20,74,327,9" Background="#FF272727" BorderBrush="{x:Null}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid  VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ItemsControl Padding="10">
                    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                        <local:SelectorImpresiones FicheroOrigen="{Binding RutaFichero}" NumeroDeCopias="{Binding NumeroCopias}" Width="200" Height ="250" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
                    </StackPanel>
                </ItemsControl>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

The onlyway I get the items to be added and correctly displayed is using the following code:我获得要添加和正确显示的项目的唯一方法是使用以下代码:

lvListaImagenes.ItemsSource = ListaImagenPedido;

But, in that case I cannot get the values to be updated when I modify them within the control.但是,在这种情况下,当我在控件中修改它们时,我无法获得要更新的值。

Hope you can help.希望你能帮忙。 I know there will be lots of issues.我知道会有很多问题。 I'm not a professional programmer and just make some applications for my own use.我不是专业的程序员,只是制作一些应用程序供我自己使用。

You have to set the DataContext property of your Window or Control.您必须设置窗口或控件的DataContext属性。 ItemSource is set by your XAML code. ItemSource由您的 XAML 代码设置。

You must not use a (mutable) struct as item type, because structs are value types.您不能使用(可变) struct作为项目类型,因为结构是值类型。

When you insert or retrieve a struct instance to/from a collection, you will only get a copy instead of a reference to the underlying object.当您向/从集合中插入或检索结构实例时,您只会获得副本而不是对基础对象的引用。

Use a regular class instead:改用常规类:

public class ImagenPedido
{
    public int IDFoto { get; set; }
    public uint NumeroCopias { get; set; }
    public string RutaFichero { get; set; }
}

In case the UI is supposed to update on property changes of the item object, the item class should also implement the INotifyPropertyChanged interface.如果 UI 应该根据 item 对象的属性更改进行更新,则 item 类还应该实现 INotifyPropertyChanged 接口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM