开发者

WPF ListView SelectedValue not being set

I've looked at a few solutions but nothing has worked yet for me.

I'm using MVVM for this project and have a ListView that I can't set the SelectedItem property.

If this is my (simplified) XAML.

<ListView Name="uxPackageGroups" ItemsSource="{Binding Path=PackageGroups, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" BorderThickness="0"
      B开发者_开发百科orderBrush="#FF0000E8" ScrollViewer.CanContentScroll="True"
      SelectedItem="{Binding Path=PackageGroupSelectedItem, Mode=TwoWay}" >
<ListView.ItemTemplate>
    <DataTemplate>
       <Label Content="{Binding Name}" Height="20" Margin="0" Padding="0"/>
    </DataTemplate>
</ListView.ItemTemplate>

And I bind it to a PackageGroups in my ViewModel

   public PackageGroup PackageGroupSelectedItem {get; set; }
   public ObservableCollection<PackageGroup> PackageGroups {get; set; }

   private void LoadUI()
   {
        PackageGroups = Factory.LoadAllPackageGroups())

        // if I try to hard-code a pre-selected item here it doesn't work.
        // 34 is a valid ID and I see a valid object when stepping through the code

        PackageGroupSelectedItem = PackageGroup.LoadByID(db, 34);
   }

Anything glaring in my code?

Thanks.


One possible problem is that you're not implementing INotifyPropertyChanged in the PackageGroupSelectedItem property.


I've just came into the same situation and it turned out that my collection item had incorrectly implemented "Equals" method. It doesn't need to implement INotifyPropertyChanged on collection item, but Equals should be implemented correctly...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜