在ListView C#XAML中绑定词汇表的键值
我在绑定字典的元素时遇到问题。我的字典包含一个对象作为键,一个整数作为值。我必须在ListView中绑定这个字典。问题是我需要访问对象的属性才能在ListView中绑定它们。属性类型是string。我没有找到解决方案,这都是关于wpf的,但我使用Windows8和MVVM灯 这是我的代码: 我的名单:在ListView C#XAML中绑定词汇表的键值,c#,xaml,binding,windows-8,C#,Xaml,Binding,Windows 8,我在绑定字典的元素时遇到问题。我的字典包含一个对象作为键,一个整数作为值。我必须在ListView中绑定这个字典。问题是我需要访问对象的属性才能在ListView中绑定它们。属性类型是string。我没有找到解决方案,这都是关于wpf的,但我使用Windows8和MVVM灯 这是我的代码: 我的名单: <ListView ItemsSource="{Binding Path=MotDansTweet}" HorizontalAlignment="Left"
<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");
}
}
private Dictionary\u motDansTweet;
公共词典MotDansTweet
{
获取{return\u motDansTweet;}
设置
{
_motDansTweet=值;
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()
{
}
}
public类对象following followerfinal
{
公共字符串用户名{get;set;}//我需要它
公共字符串说明{get;set;}
公共字符串ProfileImageUrl{get;set;}//我需要它
public int StatusesCount{get;set;}
公共int FollowerCount{get;set;}
公共int FriendsCount{get;set;}
public int-ListedCount{get;set;}
public List ListeFollower=新列表();
公共对象如下所示followerfinal()
{
}
}
谢谢。我在Win7 WPF上编写了一些代码,但这也应该适用于您: C#:
public主窗口()
{
_motDansTweet=新字典();
_添加(新的ObjetFollowingFollowerFinal{Username=“Karl1”},1);
_添加(新的ObjetFollowingFollowerFinal{Username=“Karl2”},2);
_添加(新的ObjetFollowingFollowerFinal{Username=“Karl3”},3);
_添加(新的ObjetFollowingFollowerFinal{Username=“Karl4”},4);
资源[“MotDansTweet”]=MotDansTweet;
初始化组件();
}
XAML:
尝试指定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>
您是否遇到了错误或其他问题?
<ListView ItemsSource="{DynamicResource MotDansTweet}">
<ListView.View>
<GridView>
<GridViewColumn Header="Value"
DisplayMemberBinding="{Binding Value}" />
<GridViewColumn Header="Key"
DisplayMemberBinding="{Binding Key.Username}" />
</GridView>
</ListView.View>
</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>