簡體   English   中英

WPF綁定/依賴項屬性(列表)

[英]WPF binding / dependency property (List)

我有一個顯示2個圖像列表框的應用程序。 它們是相同的圖像,但是順序不同。 比較每個順序中的位置是相關的,因此我為每個列表框使用單獨的List。

該列表具有多個依賴項屬性,因此我可以顯示多個屬性以及圖像本身。

簡而言之,我填充了2個List實例。 ImageCrawler類中的屬性都是依賴項屬性。

我幾乎對MVVM不太了解,所以如果丑陋的話,請原諒-我使用了另一個類-ViewModel,它包含2個列表,也由dep組成。 屬性如下:

public class ViewModel : DependencyObject
{

    public List<ImageCrawler> Box1
    {
        get { return (List<ImageCrawler>)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImageProperty =
        DependencyProperty.Register("Box1", typeof(List<ImageCrawler>), typeof(ViewModel));





    public List<ImageCrawler> Box2
    {
        get { return (List<ImageCrawler>)GetValue(Image2Property); }
        set { SetValue(Image2Property, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty Image2Property =
        DependencyProperty.Register("Box2", typeof(List<ImageCrawler>), typeof(ViewModel));

}

因此,這2個列表存儲在此類中。

問題是綁定。 這是XAML中一個列表框(在另一個列表中已鏡像)的代碼:

<ListBox ItemsSource="{Binding Box1}"
         DataContext="{StaticResource ViewModel}"
         x:Name="ImageBox1"
         HorizontalAlignment="Left"
         Margin="10,109,0,10"
         Width="173"
         SelectionChanged="ImageBox1_SelectionChanged">
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}">
            <SolidColorBrush.Color>#FF3399FF</SolidColorBrush.Color>
        </SolidColorBrush>
    </ListBox.Resources>
</ListBox>

可以在以下App.xaml中使用StaticResource:

<Application.Resources>
    <local:ViewModel x:Key="ViewModel" />
</Application.Resources>

因此,這些列表的填充方式...首先,我在MainWindow中具有以下字段:

public static ViewModel view = Application.Current.FindResource("ViewModel") as ViewModel;

...據我所知,是指App.xaml中的實例。

單擊一個按鈕,因此邏輯將獲取ImageCrawler對象的列表,將其放入列表中,然后將該列表分配給上面定義的視圖變量...但是綁定不起作用:(...其他所有操作...所以我這樣做是將我剛剛獲取的列表分配給視圖:

view.Box1 = Box1;

...其中Box1是List變量...編輯:我對此部分仍然感到困惑...並且想知道這是否是一個問題。 我仍在從XAML實例創建此“視圖”變量,並將列表分配給IT ...我感覺我需要設置ViewModel的XAML實例的屬性...而不是將其獲取到變量,設置...但是不知道如何...或如何工作:/

如果我這樣做:

ImageBox1.ItemsSource = view.Box1;

...然后它完美地工作(減去使我發瘋的內存泄漏,但這是另一個故事:))...但是我似乎無法弄清楚為什么在XAML中設置ItemsSource無效。

您根本不應該設置ListBox的DataContext。 不要將ViewModel創建為資源,而直接將其分配給Window的DataContext,並讓DataContent由子元素繼承:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
...

<!-- ListBox DataContext is inherited from Window -->
<ListBox ItemsSource="{Binding Box1}" ...>

在后面的代碼中,您可以像這樣訪問ViewModel實例:

private void someMethodInMainWindow()
{
    var viewModel = (ViewModel)DataContext;
    viewModel.Box1 = ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM