Answer the question
In order to leave comments, you need to log in
Can't bind ObservableCollection to ListView (WPF)?
Hello dear forum users. The essence of the problem is described in the topic title. For some reason my ListView is not displaying anything while adding an item to the collection.
public class MySQLCommand
{
public int Number { get; set; }
public string Command { get; set; }
public int Time { get; set; }
public int Memory { get; set; }
public int Rows { get; set; }
}
public ObservableCollection<MySQLCommand> _queryInf = new ObservableCollection<MySQLCommand>();
...
public ObservableCollection<MySQLCommand> queryInf
{
get { return _queryInf; }
}
...
// Если тип пакета COM_QUERY
if (buffer[4] == 3)
{
Dispatcher.Invoke(new Action(() =>
_queryInf.Add(new MySQLCommand() {
Number = 0,
Command = Encoding.ASCII.GetString(buffer, 5, byteRec - 4),
Memory = 0,
Rows = 0,
Time = 0
})));
}
<ListView HorizontalAlignment="Left" Height="313" Margin="224,66,0,0" VerticalAlignment="Top" Width="752" x:FieldModifier="public" Name="tProfiler" ItemsSource="{Binding queryInf}">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="номер" DisplayMemberBinding="{Binding Number}"></GridViewColumn>
<GridViewColumn Header="запрос" DisplayMemberBinding="{Binding Command}"></GridViewColumn>
<GridViewColumn Header="время мс" DisplayMemberBinding="{Binding Time}"></GridViewColumn>
<GridViewColumn Header="принято кб" DisplayMemberBinding="{Binding Memory}"></GridViewColumn>
<GridViewColumn Header="строки" DisplayMemberBinding="{Binding Rows}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Answer the question
In order to leave comments, you need to log in
Most likely your ListView's Binding is set incorrectly.
Try to explicitly set, for example, in the constructor after initialization:
Everything should work out.
The question was - "why the binding does not work?". The solution might be like this.
//********************
If the queryInf property is in a partial window class, then the binding to the data source in the ListView handle can be written as follows: ItemsSource="{Binding Path=queryInf,
RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
or like this:
ItemsSource="{Binding queryInf, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}"
//** **************
And if in the partial class of the UserControl object, then, accordingly, like this:
ItemsSource="{Binding Path=queryInf,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x: Type UserControl}}}"
//********************
If you forgot how to create bindings, you can stand in the markup on the ListView element in the studio, find the ItemsSource property in the properties panel and click on the box to the right of the input field, select "Create Data Binding". The "Create Binding for..." window will open. You can experiment with bindings there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question