CTRL +双击导致'CurrentItem'变为null

时间:2012-02-22 04:49:41

标签: wpf

请检查以下示例。然后尝试'Ctrl +双击',鼠标左键或右键,两个案例的currentItem都为空。

有什么想法吗?

MainWindow.xaml

它包含一个包含5个项目的列表框。“订阅”事件被订阅以重现该问题。

  <Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        >
    <Window.Resources>
        <DrawingBrush x:Key="SEShape_Zoom_White" Stretch="Uniform">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Brush="#FFFFFFFF" Geometry="F1 M 577.975,406.653L 573.41,402.119C 575.858,398.583 575.527,393.715 572.36,390.593C 568.81,387.098 563.094,387.14 559.591,390.692C 556.096,394.239 556.143,399.96 559.69,403.458C 562.818,406.533 567.599,406.827 571.091,404.439L 575.654,408.973C 576.134,409.452 576.907,409.452 577.386,408.973L 577.975,408.385C 578.454,407.906 578.454,407.132 577.975,406.653 Z M 560.844,402.288C 557.938,399.419 557.901,394.749 560.763,391.84C 563.63,388.937 568.3,388.898 571.206,391.762C 574.118,394.627 574.147,399.299 571.287,402.202C 568.425,405.116 563.75,405.145 560.844,402.288 Z "/>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>        
    </Window.Resources>
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <ListBox
            Margin="5"
            Background="Transparent"
            ItemsSource="{Binding ListItemsSource}"
            IsSynchronizedWithCurrentItem="True">
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Tag="{Binding}" MinWidth="75" Margin="1">
                        <Rectangle Name="Rect" Margin="12, 0, 0, 0" Stroke="Green" StrokeThickness="1.25" RadiusX="3" RadiusY="3" Fill="DarkGray"/>
                        <Grid Margin="12, 2, 2, 2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Name="TBName" Grid.Column="0" Text="{Binding Value}" VerticalAlignment="Center" Foreground="Green" Margin="16, 0, 0, 0"/>

                            <Grid Grid.Column="1" x:Name="CibleGrid" VerticalAlignment="Center" Margin="10, 0, 0, 0">
                                <Canvas Name="CanvasZoom" Width="18" Height="18" Background="{DynamicResource SEShape_Zoom_White}" MouseUp="Image_MouseUp" Cursor="Hand"/>
                            </Grid>
                        </Grid>
                        <Canvas Name="ItemIcon" Grid.Column="1" Width="24" Height="24" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>        
    </StackPanel>
</Window>

MainWindow.xaml.cs

出现错误情况时,我们会收到“错误”消息

namespace WpfApplication2
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

         private ObservableCollection<KeyValuePair<string, string>> _dropDownValues = new ObservableCollection<KeyValuePair<string, string>>();

         public ICollectionView ListItemsSource
         {
             get { return privateHolderForListItemsSource; }
             set
             {                 
                 privateHolderForListItemsSource = value;

                 if (PropertyChanged != null)
                 {
                     OnPropertyChanged("ListItemsSource");
                 }
             }
         }

        private ICollectionView privateHolderForListItemsSource;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            ListItemsSource = CollectionViewSource.GetDefaultView(_dropDownValues);

            _dropDownValues.Add(new KeyValuePair<string, string>("672", "Jim Smith"));
            _dropDownValues.Add(new KeyValuePair<string, string>("281", "James Anders"));
            _dropDownValues.Add(new KeyValuePair<string, string>("321", "Angie Wonderson"));
            _dropDownValues.Add(new KeyValuePair<string, string>("221", "Hal Cloud"));
            _dropDownValues.Add(new KeyValuePair<string, string>("123", "Hugh Brandley"));            
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }       

        private void Image_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (ListItemsSource.CurrentItem == null)
            {
                MessageBox.Show("Error");
            }
        }
    }
}

0 个答案:

没有答案