scottheckel
scottheckel

Reputation: 9244

WPF GridViewColumn's DisplayMemberBinding using a Dictionary Key's Value

I haven't found anything similiar to this on Stack or Google, so maybe it isnt' possible, but hopefully someone smart will have an idea. I'm a bit of a noobie to WPF/XAML.

I have a custom class that resembles something similiar to this.

public class LogEntry
{
   public Diciontary<string, string> Stuff;
   public string MyOtherProperty;   
}

My GridView will have 2 columns. One for MyOtherProperty and one for Stuff["Stuff1"]. Assuming I cannot change the Diciontary to something a lot easier to bind to.

I am binding my ListView to a List<LogEntry>. How would I accomplish it in this scenario.

<ListView ItemsSource="{Binding}" DataContext="{Binding}">
   <ListView.View>
      <GridView>
         <GridView.Columns>
            <GridViewColumn DisplayMemberBinding="{Binding MyOtherProperty}"></GridViewColumn>
            <GridViewColumn DisplayMemberBinding="**{Binding Stuff[Stuff1]}**"></GridViewColumn>
         </GridView.Columns>
      </GridView>
   </ListView.View>
</ListView>

Any ideas? Thanks.

Upvotes: 3

Views: 5988

Answers (1)

Wallstreet Programmer
Wallstreet Programmer

Reputation: 9677

WPF supports binding to properties and not fields. Change LogEntry class to below and it should work.

public class LogEntry
{
    public Dictionary<string, string> Stuff { get; set; }
    public string MyOtherProperty { get; set; }
}

Upvotes: 4

Related Questions