0

我在绑定我的字典的元素时遇到问题。我的字典包含一个对象作为键和一个整数作为值。我必须在 ListView 中绑定这个字典。问题是我需要访问我的对象的属性来绑定它们在 listview.Properties 类型是字符串。我没有找到解决方案,这都是关于 wpf 但我正在使用带有 MVVM Light 的 Windows8。

有我的代码:

我的列表 :

<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>

我的字典:

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

我的对象:

    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()
    {

    }
}

谢谢。

4

2 回答 2

1

我在 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>
于 2013-04-19T09:41:54.527 回答
0

尝试指定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>
于 2013-04-19T09:49:08.623 回答