簡體   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