我需要在一个ListBox
我可以实现的水平方向显示一组图像。但是,这里面临的挑战是,当我单击或选择其中的任何图像并在代码隐藏中获取所选图像时,无法将所选项目 ( Image
) 绑定到项目属性。ListBox-selected
ListBox
以下是我的 XAML 代码,稍后是我的XAML.cs
代码。如果我缺少解决问题的方法,请告诉我。
谢谢。
<ListBox Grid.Row="1" x:Name="ThumbnailPanel" ItemsSource="{Binding Frames}"
SelectedItem="{Binding Path=MediaFrame,Mode=TwoWay}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="20" Rows="1">
</UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public ObservableCollection<Image> Frames { get; set; }
public Image MediaFrame
{
get
{
return mediaFrame;
}
set
{
if (mediaFrame != value)
{
mediaFrame = value;
OnPropertyChanged("MediaFrame");
}
}
}
private Image mediaFrame;
if (Frames == null)
{
Frames = new ObservableCollection<Image>();
}
Frames.Add(
new Image
{
Source = imageSource,
ToolTip = _mediaPlayer.Position,
MaxWidth = _mediaPlayer.NaturalVideoWidth,
MaxHeight = _mediaPlayer.NaturalVideoHeight,
Margin = new Thickness(2)
});
ThumbnailPanel.DataContext = Frames;