首页 > 其他分享 >WPF ListBox IsSynchronizedWithCurrentItem="True"

WPF ListBox IsSynchronizedWithCurrentItem="True"

时间:2024-10-05 20:34:56浏览次数:7  
标签:get Windows imgsList System IsSynchronizedWithCurrentItem using WPF True public

//xaml
<Window x:Class="WpfApp13.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        WindowState="Maximized"
        xmlns:local="clr-namespace:WpfApp13"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:BookVM/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 IsSynchronizedWithCurrentItem="True"
                 DisplayMemberPath="Id"
                 />
        <ListBox Grid.Column="1" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 IsSynchronizedWithCurrentItem="True"
                 DisplayMemberPath="Name"
                 />
        <ListBox Grid.Column="2" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 IsSynchronizedWithCurrentItem="True"
                 >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding DataContext.ImgUrl,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"
                           Width="20"
                           Height="50" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>



//.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApp13
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class BookVM : INotifyPropertyChanged
    {
        public BookVM()
        {
            InitData();
        }

        private void InitData()
        {
            var imgsList = new List<string>(Directory.GetFiles(@"../../Images"));
            if(imgsList!=null && imgsList.Any())
            {
                int imgsCount=imgsList.Count;
                BooksCollection = new ObservableCollection<Book>();
                for(int i=0;i<10000;i++)
                {
                    BooksCollection.Add(new Book()
                    {
                        Id=i+1,
                        Name=$"Name_{i+1}",
                        ImgUrl = $"{imgsList[i%imgsCount]}"
                    });
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if(value!=booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged(nameof(BooksCollection));
                }
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string ImgUrl { get; set; }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:get,Windows,imgsList,System,IsSynchronizedWithCurrentItem,using,WPF,True,public
From: https://www.cnblogs.com/Fred1987/p/18448437

相关文章

  • WPF VirtualizingPanel.CacheLength="1" VirtualizingPanel.CacheLengthUnit="Item" c
    <ListBoxItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectedIndex="{BindingImgIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"VirtualizingPanel.IsVirtualizing=......
  • WPF 闪烁动画
    实现使用一个Boder控件,通过后台属性控制Boder的背景色变化,实现闪烁的效果。1.xaml视图代码<BorderWidth="15"Height="15"Margin="25000"><Border.Style><StyleTargetType="{x:TypeBorder}"><SetterPro......
  • WPF ListBox ListBoxItemTemplate display image automatically via System.Timers.Ti
    //xaml<Windowx:Class="WpfApp6.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • WPF Datagrid display via DataGridTemplateColumn
    <DataGridTemplateColumnHeader="Image"><DataGridTemplateColumn.CellTemplate><DataTemplate><ImageSource="{BindingDataContext.ImgUrl,RelativeSource={RelativeSourceMode=F......
  • WPF FindResource,Resource[key] SystemColors TryFindResource
    //xaml<Windowx:Class="WpfApp3.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • WPF image via web url or uri
    Thebasicroadmapistodownloadwebimageatfirst,second convertitintomemeorystream,thirdassignthememorystreamtobitmapimageasStreamSource. //xaml<Windowx:Class="WpfApp2.MainWindow"xmlns="http://schemas.micro......
  • WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
    1.Install Microsoft.Xaml.Behaviors.Wpf  2.<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseDown"><behavior:InvokeCommandActionCommand="{BindingClickCommand}"......
  • WPF MVVM behavior CallMethodAction InvokeCommandAction
    1.Install  Microsoft.Xaml.Behaviors.Wpf 2.xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMet......
  • 欢乐国庆:SciChart 8.5 for WPF Crack
    WPFChartPerformanceSciChart’slegendaryWPFChartperformanceisenabledbyourproprietary,in-house,C++crossplatform3Dgraphicsengine,VisualXccelerator.TheengineisentirelyhardwareacceleratedtargettingDirectXonWindowsplatform.Youw......
  • (六)WPF数据驱动模式
     WPF开发方式; MVVM(ModelViewViewModel)1.绑定XAML数据方式  在 XAML中添加绑定数据和绑定的操作属性        Content="{BindingMyVar}" 在XAML对应了的窗体类的构造函数添加数据绑定        this.DataContext=mainViewModel;//让此页面的数据取......