简体   繁体   中英

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. 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. I've tried to Bind it from XAML but I cannot make it work.

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

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 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. ItemSource is set by your XAML code.

You must not use a (mutable) struct as item type, because structs are value types.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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