当我尝试将可观察列表与列表框/视图同步时,我遇到此问题 它显示第一个项目X次(x列表中的记录总数) 但它不会改变变量的
这是XAML
<ListBox x:Name="PostListView" BorderThickness="0"
MinHeight="300"
Background="{x:Null}"
BorderBrush="{x:Null}"
Foreground="{x:Null}"
VerticalContentAlignment="Top"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
DataContext="{Binding Source={StaticResource PostListData}}"
ItemsSource="{Binding Mode=OneWay}"
IsSynchronizedWithCurrentItem="True"
MinWidth="332" SelectedIndex="0" SelectionMode="Extended" AlternationCount="1">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel x:Name="SinglePost" VerticalAlignment="Top" ScrollViewer.CanContentScroll="True" ClipToBounds="True" Width="333" Height="70" d:LayoutOverrides="VerticalAlignment" d:IsEffectDisabled="True">
<DockPanel.DataContext>
<local:PostList/>
</DockPanel.DataContext>
<StackPanel x:Name="AvatarNickHolder" Width="60">
<Label x:Name="Nick" HorizontalAlignment="Center" Margin="5,0" VerticalAlignment="Top" Height="15" Content="{Binding Path=pUsername, FallbackValue=pUsername}" FontFamily="Arial" FontSize="10.667" Padding="5,0"/>
<Image x:Name="Avatar" HorizontalAlignment="Center" Margin="5,0,5,5" VerticalAlignment="Top" Width="50" Height="50" IsHitTestVisible="False" Source="1045443356IMG_0972.jpg" Stretch="UniformToFill"/>
</StackPanel>
<TextBlock x:Name="userPostText" Margin="0,0,5,0" VerticalAlignment="Center" FontSize="10.667" Text="{Binding Path=pMsg, FallbackValue=pMsg}" TextWrapping="Wrap"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这里是ovservable列表类
public class PostList : ObservableCollection<PostData>
{
public PostList()
: base()
{
Add(new PostData("this is test msg", "Cather", "1045443356IMG_0972.jpg"));
Add(new PostData("this is test msg1", "t1", "1045443356IMG_0972.jpg"));
Add(new PostData("this is test msg2", "t2", "1045443356IMG_0972.jpg"));
Add(new PostData("this is test msg3", "t3", "1045443356IMG_0972.jpg"));
Add(new PostData("this is test msg4", "t4", "1045443356IMG_0972.jpg"));
Add(new PostData("this is test msg5", "t5", "1045443356IMG_0972.jpg"));
// Add(new PostData("Isak", "Dinesen"));
// Add(new PostData("Victor", "Hugo"));
// Add(new PostData("Jules", "Verne"));
}
}
public class PostData
{
private string Username;
private string Msg;
private string Avatar;
private string LinkAttached;
private string PicAttached;
private string VideoAttached;
public PostData(string msg ,string username, string avatar=null, string link=null,string pic=null ,string video=null)
{
this.Username = username;
this.Msg = msg;
this.Avatar = avatar;
this.LinkAttached = link;
this.PicAttached = pic;
this.VideoAttached = video;
}
public string pMsg
{
get { return Msg; }
set { Msg = value; }
}
public string pUsername
{
get { return Username; }
set { Username = value; }
}
public string pAvatar
{
get { return Avatar; }
set { Avatar = value; }
}
public string pLink
{
get { return LinkAttached; }
set { LinkAttached = value; }
}
public string pPic
{
get { return PicAttached; }
set { PicAttached = value; }
}
public string pVideo
{
get { return VideoAttached; }
set { VideoAttached = value; }
}
}
有什么想法吗?
答案 0 :(得分:1)
每次创建模板时,您都在创建一个新的DataContext(PostList)。
在ListBox中,将ItemsSource设置为新的PostList。
ListBox会将每个PostData实例分配给DataTemplate的每个副本,并将其设置为根元素的DataContext。
tl; dr:摆脱
<DockPanel.DataContext>
<local:PostList/>
</DockPanel.DataContext>