繁体   English   中英

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

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

我正在尝试制作一个供我自己使用的应用程序,并且需要使用自定义控件作为 ListView 的项目。 该控件包含一个图像和该图像要打印的副本数。

如果我从代码中绑定它,我可以控制显示图片和副本数量,如我将显示的那样,但在这种情况下,我无法在 ListView 的项目中获取要更新的副本数量。 我试图从 XAML 绑定它,但我无法让它工作。

这是控件的相关代码:

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); }
    }

修改要打印的份数时,控件中的值和属性会得到正确更新。

这是我试图绑定的 ObervableCollection 中包含的结构

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

这就是我尝试在 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>

我获得要添加和正确显示的项目的唯一方法是使用以下代码:

lvListaImagenes.ItemsSource = ListaImagenPedido;

但是,在这种情况下,当我在控件中修改它们时,我无法获得要更新的值。

希望你能帮忙。 我知道会有很多问题。 我不是专业的程序员,只是制作一些应用程序供我自己使用。

您必须设置窗口或控件的DataContext属性。 ItemSource由您的 XAML 代码设置。

您不能使用(可变) struct作为项目类型,因为结构是值类型。

当您向/从集合中插入或检索结构实例时,您只会获得副本而不是对基础对象的引用。

改用常规类:

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

如果 UI 应该根据 item 对象的属性更改进行更新,则 item 类还应该实现 INotifyPropertyChanged 接口。

暂无
暂无

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

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