0

リストコントロールがあり、各アイテムには2つの画像とテキストが含まれています。各アイテムをクリックすると、選択したリストアイテムの選択した画像を非表示または表示します。

XAMLコードスニペットは次のとおりです。

            <ListBox x:Name="list" SelectionChanged="list_SelectionChanged"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <Image Source="{Binding ImagePath}" Stretch="None"/>
                        <Image Source="{Binding ImagePath}" Stretch="None"  
                               Visibility="{Binding ImageVisibility, 
                            Converter={StaticResource boolVisibilityConverter}}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

C#コード:

 dataSource = new ObservableCollection<ImageData>() 
        { 
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = false},
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true},
        new ImageData(){Name = "User1:", ImagePath="/Images/user1.png", ImageVisibility = true},
        new ImageData(){Name = "User2:", ImagePath="/Images/user2.png", ImageVisibility = true}
        };

リスト選択変更イベント:

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ((ImageData)(((object[])(e.AddedItems))[0])).ImageVisibility = false;
        list.UpdateLayout();
    }

ImageDataクラス:

 public class ImageData
    {
        public string ImagePath { get; set; }
        public string Name { get; set; }
        public bool ImageVisibility { get; set; }
    }

画像可視性コンバーター:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool flag = false;
        if (value is bool)
        {
            flag = (bool)value;
        }
        else if (value is bool?)
        {
            bool? nullable = (bool?)value;
            flag = nullable.HasValue ? nullable.Value : false;
        }
        return (flag ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
    }

}

そのような機能を実現するために私を助けてください。

4

1 に答える 1

0

INotifyPropertyChangedインターフェイスhttp://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspxを使用する必要があります

たとえば、次のようになります。

public class ImageData : INotifyPropertyChanged
{
    private bool _imageVisibility;
    public string ImagePath { get; set; }
    public string Name { get; set; }
    public bool ImageVisibility
    {
        get
        {
            return _imageVisibility;
        }
        set
        {
            _imageVisibility = value;
            OnPropertyChanged("ImageVisibility");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

あなたがここで見ることができるあなたのタスクに応じた完全な変更(ドロップボックス)

于 2013-03-06T07:19:59.130 に答える