簡體   English   中英

List中的綁定imageSource屬性更改,而該更改又在列表中托管的對象內部

[英]Binding imageSource property change in List which is in-turn inside an object which is hosted in a list

考慮一個包含自定義對象的列表框(我們稱它們為“ items”),該列表框又包括一個對象的List<T> (我們稱它們為“ options”)。 我想在listbox每個項目上顯示一個標記圖像,該圖像基本上表示“項目的所有選項均已設置”。

項目的布局在xamldataTemplate中定義。 基本上有一行:

<Image Source="{Binding options, Converter={StaticResource conv}, UpdateSourceTrigger=LostFocus}"/>

它將圖像源綁定到對象中前面提到的選項列表

轉換器的定義如下:

using Project.Model.Option;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace Project.ViewModel.Converter
{
    class cStatusImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                BitmapImage failImage = new BitmapImage();
                failImage.BeginInit();
                failImage.UriSource = new Uri("pack://application:,,,/view/Icons/failImage.png");
                failImage.EndInit();

                List<cOption> options = (List<cOption>)value;

                for (int i = 0; i < options.Count; i ++)
                {
                    if (options[i].value == null)
                        return failImage;
                }

                BitmapImage successImage = new BitmapImage();
                successImage.BeginInit();
                successImage.UriSource = new Uri("pack://application:,,,/view/Icons/successImage.png");
                successImage.EndInit();

                return successImage;
            }
            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

最后在選項定義中

// value of option
    private string _value;
    public string value
    {
        get
        {
            return _value;
        }
        set
        {
            if (_value!= value)
            {
                _value = value;
                OnPropertyChanged("options");
            }
        }
    }

可能很簡單:僅在將項目添加到列表時才調用轉換器,但是當我觸發OnPropertyChanged時,當我更改options對象的value字段時不會調用該轉換器...

顯示“ failImage”(因此原則上代碼正在工作),但是它不會更新...因此,在觸發OnPropertyChanged事件(?)時我做錯了什么

任何幫助表示贊賞

開始編輯08.10.2015 10.02:

“ item”的定義:

using System;
using Project.Model.Option;
using System.Collections.Generic;

namespace Project.Model
{
    [Serializable]
    class Item: ModelBase
    {
        #region properties
        // label of item
        public string label { get; set; }

        // item options
        public List<cOption> options {get; set;};
        #endregion
    }
}

結束編輯08.10.2015 10.02

開始編輯09.10.2015 14.24

現在,如果我將xml標簽放在窗口中的某個位置並指向當前選中的項目,則此方法有效

<Image Grid.Row="2" Grid.Column="1"  Source="{Binding SelectedItem.AllOptionsSet, ElementName=lstSequence, Converter={StaticResource sic}, UpdateSourceTrigger=LostFocus}" Width="15" Height="15"/> 

但是一旦我將代碼放入dataTemplate (如前所述...)並像這樣進行綁定

<Image Source="{Binding AllOptionsSet, Converter={StaticResource sic}, UpdateSourceTrigger=LostFocus}" Width="15" Height="15"/>

它只是不進入轉換器...另一方面,輸出窗口沒有顯示綁定錯誤。

結束編輯09.10.2015 14.24

開始編輯2015年9月10日16.10

因此,我認為我將其歸結為以下問題:盡管觸發了該屬性的NotifyPropertyChanged事件,但未執行屬性的吸氣劑的事實可能是什么原因?

我真的很堅持。 有人嗎

結束編輯09.10.2015 16.10

開始編輯2015年9月10日17.19

因此,我只是通過在datatemplate中設置樣式觸發器來擺脫了轉換器:

<Image Width="15" Height="15" >
    <Image.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding AllOptionsSet}" Value="True">
                    <Setter Property="Image.Source" Value="Icons/successImage.png"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding AllOptionsSet}" Value="False">
                    <Setter Property="Image.Source" Value="Icons/failImage.png"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

因此,這也會正確設置“ failImage”(因為將項目添加到列表時,所有選項的所有“值”字段均為空)。 當我調用INPC(AllOptionsSet)時,它只是沒有更新。 因此,基本上,GUI似乎沒有“監聽”事件(?)

結束編輯09.10.2015 17.19

您的問題是因為您要綁定到options (它是一個List<T> ,但是您正在該列表包含的cOption項上觸發INPC。

您不會在list本身上觸發INPC (或INotifyCollectionChanged ),因此綁定永遠不會更新。

暫無
暫無

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

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