0

我需要在一个ListBox我可以实现的水平方向显示一组图像。但是,这里面临的挑战是,当我单击或选择其中的任何图像并在代码隐藏中获取所选图像时,无法将所选项目 ( Image) 绑定到项目属性。ListBox-selectedListBox

以下是我的 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;
4

1 回答 1

0

不是很清楚,但是当你写的时候

ThumbnailPanel.DataContext = Frames;

那是Frames财产吗?那么ItemsSource="{Binding Frames}"绑定将如何工作?我想您需要将 设置为DataContext具有FramesandMediaFrame属性的对象。也许:

ThumbnailPanel.DataContext = this;
于 2013-02-05T10:50:38.563 回答