简体   繁体   English

WPF:一种从ListBox绑定到源的方法

[英]WPF: One way binding to source from ListBox

I have a ListBox whose ItemSource is a List where X is defined as: 我有一个ListBox,其ItemSource是一个列表,其中X定义为:

public class X
{
    public string FullPath { get; set; }
}

My ViewModel is 我的ViewModel是

public class ViewModel
{
    public List<X> MyList { get; set; }
    public X MyItem { get; set; }
    public ViewModel()
    {
        MyList = new List<X>
        {
            new X { FullPath = "q:\\temp\\x1.png"},
            new X { FullPath = "q:\\temp\\x2.png"}
        };
    }
}

And XAML: 和XAML:

<Window x:Class="Bind1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Bind1"
        Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>
    <StackPanel>
        <Image Source="{Binding MyItem.FullPath, Mode=OneWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="None"/>
        <ListBox ItemsSource="{Binding MyList}" 
                    SelectedItem="{Binding MyItem, Mode=OneWayToSource}"
                    SelectionMode="Single">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FullPath}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Window>

This works when I used the keyboard up and down arrows in the ListBox (the image from the path selected in the ListBox shows up in the <Image>). 当我在ListBox中使用键盘的向上和向下箭头时,此功能有效(来自ListBox中所选路径的图像显示在<Image>中)。 However, when I click an item with the mouse, the item I click is not selected in the ListBox (the mouse click has no effect). 但是,当我用鼠标单击一个项目时,在列表框中没有选择我单击的项目(鼠标单击无效)。 The correct image shows up in the image very briefly (a fraction of a second) then the original image is displayed. 正确的图像会非常短暂地显示在图像中(几分之一秒),然后显示原始图像。 What am I doing wrong? 我究竟做错了什么?

Since you are using StackPanel , while the Image changes upon selection change in the ListBox , the size of the Image control may change, hence also the location of the ListBox . 由于您正在使用StackPanel ,因此当ListBoxImage随选择更改而更改时, Image控件的大小可能会更改,因此ListBox的位置也会更改。

Such case may cause an effect that the list item you click on not be the same item when you release the mouse button, as the item location changed. 这种情况可能会导致以下效果:放开鼠标按钮时,单击的列表项可能与项目位置不同,因此与单击的列表项不同。

There are many workaround, eg fix the size of the Image control, use Grid to define a layout, or simply put the Image after the ListBox in the StackPanel 有许多解决方法,例如,固定Image控件的大小,使用Grid定义布局或将Image放在StackPanelListBox之后

I could reproduce the issue and you can fix the image size or move the image control below the listbox. 我可以重现该问题,您可以固定图像大小或将图像控件移到列表框下方。 I agree with Evan 我同意埃文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM