列表框中的 ObservableCollection 图像到内容控制主详细信息 WPf

发布于 2024-11-10 04:15:54 字数 985 浏览 4 评论 0原文

我有一个通过以下代码填充的可观察图像集合:

<StackPanel Orientation="Horizontal" Grid.Column="0">
  <ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True"
           SelectedIndex="0" SelectedItem="{Binding CurrentItem}" />
</StackPanel>
<ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}"
                Margin="9,0,0,0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top"/>

但是内容控件应该通过 ObservableCollection 绑定到 BigImageView

 BigImage = new ObservableCollection<Image>();

 _listView = CollectionViewSource.GetDefaultView(BigImage);
 _listView.CurrentChanged += new EventHandler(OnCurrentChanged);

 public System.ComponentModel.ICollectionView BigImageView
 {
   get
   {
     return _listView;
   }
   set
   {
     _listView = value;
     OnPropertyChanged("BigImageView");
   }
 }

我想在移动列表框时将图像返回到内容控件。我绞尽脑汁,想尽一切办法,但还是不行。任何帮助将不胜感激。

I have an observablecollection of Images that get populated via the following code:

<StackPanel Orientation="Horizontal" Grid.Column="0">
  <ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True"
           SelectedIndex="0" SelectedItem="{Binding CurrentItem}" />
</StackPanel>
<ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}"
                Margin="9,0,0,0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top"/>

However the Content Control is supposed to bind to the BigImageView via an ObservableCollection

 BigImage = new ObservableCollection<Image>();

 _listView = CollectionViewSource.GetDefaultView(BigImage);
 _listView.CurrentChanged += new EventHandler(OnCurrentChanged);

 public System.ComponentModel.ICollectionView BigImageView
 {
   get
   {
     return _listView;
   }
   set
   {
     _listView = value;
     OnPropertyChanged("BigImageView");
   }
 }

I want to return the image to the content control when I move the listbox. I have been racking my brain and trying everyhitn but it does not work. any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-11-17 04:15:54

不需要绑定所选项目,collectionview 应该处理这个问题。

试试这个:

 <ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True" />

 <ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}" VerticalAlignment="Top">

    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Image Source="{Binding}"/>
        </DataTemplate>
    <ContentControl.ContentTemplate>

There is no need to bind the selecteditem, the collectionview should take care of that.

Try this:

 <ListBox ItemsSource="{Binding BigImageView}" IsSynchronizedWithCurrentItem="True" />

 <ContentControl Name="Detail" Content="{Binding BigImageView, Mode=OneWay}" VerticalAlignment="Top">

    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Image Source="{Binding}"/>
        </DataTemplate>
    <ContentControl.ContentTemplate>

温暖的光 2024-11-17 04:15:54

1

创建一个包含列表和所选项目的视图模型:

public class BigImageViewModel : INotifyPropertyChanged
{ 
     private string bigImage;

     //string for path? 
     public ObservableCollection<string> BigImageView {get; set; } //Of course, make sure it has a value

     public string SelectedBigImage          
     {
         get { return bigImage; } 
         set { bigImage = values; NotifyPropertyChanged("SelectedBigImage"); }
     }
}

在构造函数中在控件的 DataContext 上设置此对象:

DataContext = new BigImage(); //Make sure you initialize your list

将 ListBox ItemsSource 设置为 BigImage 列表,将 SelectedItem 绑定到 BigImageView
并在内容控件中使用它:

<ListBox ItemsSource="{Binding BigImageView}" SelectedItem={Binding SelectedBigImage} />

ContentControl:

<ContentControl Name="Detail" Content="{Binding SelectedBigImage, Mode=OneWay}" VerticalAlignment="Top">

   <ContentControl.ContentTemplate>
       <DataTemplate>
           <Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
       </DataTemplate>
   <ContentControl.ContentTemplate>
</ContentControl>

2

或者拧紧视图模型:

直接在构造函数中设置列表(在 InitializeComponent() 之后):

myListBox.ItemsSource = ObservableCollection<string>(); //Make sure you initialize your list with whatever your object is..

为列表命名:

并使用 ElementName 绑定到您所选的项目:

<ContentControl Name="Detail" Content="{Binding ElementName=myListBox, Path=SelectedItem}" VerticalAlignment="Top">

   <ContentControl.ContentTemplate>
       <DataTemplate>
           <Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
       </DataTemplate>
   <ContentControl.ContentTemplate>
</ContentControl>

1

Create a viewmodel with a list and a selected item:

public class BigImageViewModel : INotifyPropertyChanged
{ 
     private string bigImage;

     //string for path? 
     public ObservableCollection<string> BigImageView {get; set; } //Of course, make sure it has a value

     public string SelectedBigImage          
     {
         get { return bigImage; } 
         set { bigImage = values; NotifyPropertyChanged("SelectedBigImage"); }
     }
}

Set this object on the DataContext of your control in the constructor:

DataContext = new BigImage(); //Make sure you initialize your list

Set the ListBox ItemsSource to your BigImage list, bind your SelectedItem to BigImageView
and use that in your content control:

<ListBox ItemsSource="{Binding BigImageView}" SelectedItem={Binding SelectedBigImage} />

ContentControl:

<ContentControl Name="Detail" Content="{Binding SelectedBigImage, Mode=OneWay}" VerticalAlignment="Top">

   <ContentControl.ContentTemplate>
       <DataTemplate>
           <Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
       </DataTemplate>
   <ContentControl.ContentTemplate>
</ContentControl>

2

Or screw that view model:

Set the list directly in the constructor (after the InitializeComponent() ):

myListBox.ItemsSource = ObservableCollection<string>(); //Make sure you initialize your list with whatever your object is..

Give the list a name:

And bind with an ElementName binding to your selected item:

<ContentControl Name="Detail" Content="{Binding ElementName=myListBox, Path=SelectedItem}" VerticalAlignment="Top">

   <ContentControl.ContentTemplate>
       <DataTemplate>
           <Image Source="{Binding}"/> <!-- Nice template for showing your string BigImage -->
       </DataTemplate>
   <ContentControl.ContentTemplate>
</ContentControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文