在ListView c# XAML中绑定字典的键值

本文关键字:字典 键值 绑定 ListView XAML | 更新日期: 2023-09-27 18:10:04

我在绑定字典元素时遇到了一个问题。我的字典包含一个作为键的对象和一个作为值的整数。我需要在ListView中绑定这个字典。问题是,我需要到达我的对象的属性绑定到列表视图。属性类型为字符串。我没有找到解决方案,这都是关于wpf的,但我使用的是Windows8与MVVM Light。

这是我的代码:

My List:

<ListView ItemsSource="{Binding Path=MotDansTweet}"
          HorizontalAlignment="Left"
          Height="570"
          Margin="64,28,0,0"
          VerticalAlignment="Top"
          Width="350"
          Background="#FF009BB4"
          Grid.Column="1">
  <StackPanel Height="118"
              Width="340">
    <Grid Height="100">
      <Grid HorizontalAlignment="Left"
            Height="122"
            VerticalAlignment="Top"
            Width="330"
            Margin="0,0,0,-22">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="137*" />
          <ColumnDefinition Width="193*" />
        </Grid.ColumnDefinitions>
        <Image Source="{Binding Path=Key.ProfileImageUrl }"
               HorizontalAlignment="Left"
               Height="112"
               VerticalAlignment="Top"
               Width="127" />
        <TextBlock Text="{Binding Path=Key.Username}"
                   Grid.Column="1"
                   HorizontalAlignment="Left"
                   Margin="10,10,0,0"
                   TextWrapping="Wrap"
                   VerticalAlignment="Top"
                   Height="46"
                   Width="173" />
        <TextBlock Text="{Binding Path=Values}"
                   Grid.Column="1"
                   HorizontalAlignment="Left"
                   Margin="10,61,0,0"
                   TextWrapping="Wrap"
                   VerticalAlignment="Top"
                   Height="41"
                   Width="154" />
      </Grid>
    </Grid>
  </StackPanel>
</ListView>

My dictionary:

 private Dictionary<ObjetFollowingFollowerFinal,int> _motDansTweet ;
    public Dictionary<ObjetFollowingFollowerFinal, int> MotDansTweet
    {
        get { return _motDansTweet; }
        set
        {
            _motDansTweet = value;
            RaisePropertyChanged("MotDansTweet");
        }
    }

My Object:

    public class ObjetFollowingFollowerFinal
{
    public string Username { get; set; } // I need it
    public string Description { get; set; }
    public string ProfileImageUrl { get; set; } //I need it
    public int StatusesCount { get; set; }
    public int FollowerCount { get; set; }
    public int FriendsCount { get; set; }
    public int ListedCount { get; set; }
    public List<User> ListeFollower = new List<User>(); 
    public ObjetFollowingFollowerFinal()
    {
    }
}

谢谢。

在ListView c# XAML中绑定字典的键值

我在Win7 WPF上编写了一些东西,但这也应该适用于您:

c#:

public MainWindow()
        {
            _motDansTweet = new Dictionary<ObjetFollowingFollowerFinal, int>();
            _motDansTweet.Add(new ObjetFollowingFollowerFinal { Username = "Karl1" }, 1);
            _motDansTweet.Add(new ObjetFollowingFollowerFinal { Username = "Karl2" }, 2);
            _motDansTweet.Add(new ObjetFollowingFollowerFinal { Username = "Karl3" }, 3);
            _motDansTweet.Add(new ObjetFollowingFollowerFinal { Username = "Karl4" }, 4);
            Resources["MotDansTweet"] = MotDansTweet;
            InitializeComponent();
        }
XAML:

<ListView ItemsSource="{DynamicResource MotDansTweet}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Value"
                            DisplayMemberBinding="{Binding Value}" />
                    <GridViewColumn Header="Key"
                            DisplayMemberBinding="{Binding Key.Username}" />
                </GridView>
            </ListView.View>
        </ListView>

尝试指定ItemTemplate而不是直接将StackPanel放在ListView中:

<ListView ItemsSource="{Binding Path=MotDansTweet}"
    HorizontalAlignment="Left" Height="570" Margin="64,28,0,0" VerticalAlignment="Top" Width="350" Background="#FF009BB4" Grid.Column="1">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Height="118" Width="340">
                <Grid Height="100">
                    <Grid HorizontalAlignment="Left" Height="122" VerticalAlignment="Top" Width="330" Margin="0,0,0,-22">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="137*"/>
                            <ColumnDefinition Width="193*"/>
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding Path=Key.ProfileImageUrl }" HorizontalAlignment="Left" Height="112" VerticalAlignment="Top" Width="127"/>
                        <TextBlock Text="{Binding Path=Key.Username}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="46" Width="173"/>
                        <TextBlock Text="{Binding Path=Values}" Grid.Column="1" HorizontalAlignment="Left" Margin="10,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="41" Width="154"/>
                    </Grid>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>