WPF ListBox/View Data Binding weird result
I have this problem when i try to synchronize a observable list with listbox/view it displays the first item X times (x total amount of records in the list) but it doesn't change the variable's
here is the 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开发者_JS百科="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>
and here is the ovservable list class
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; }
}
}
Any ideas ?
You are creating a new DataContext (PostList) every time a template is created.
In the ListBox, set the ItemsSource to a new PostList.
The ListBox will assign each instance of PostData to each copy of the DataTemplate and set it as the DataContext of the root element.
tl;dr: Get rid of
<DockPanel.DataContext>
<local:PostList/>
</DockPanel.DataContext>
精彩评论