列表视图绑定不更新视图 [英] Listview binding doesn't update the view
问题描述
我有这种设置为我的的ListView
,我试图做的绑定的在我的MVVM结构工作。
I have this setup for my ListView
and I am trying to make Binding to work in my MVVM structure.
这是 XAML
code:
<Window.Resources>
<CollectionViewSource
x:Key="DeviceList"
Source="{Binding Path=DiscoveredDevicesList}">
</CollectionViewSource>
</Window.Resources>
<ListView
Grid.Row="1"
Width="500"
HorizontalAlignment="Left"
Margin="10"
Grid.Column="1"
DataContext="{StaticResource DeviceList}"
ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="Device name" DisplayMemberBinding="{Binding Path=DeviceName}"/>
<GridViewColumn Header="Rssi" DisplayMemberBinding="{Binding Path=Rssi}"/>
<GridViewColumn Header="GPS Row" DisplayMemberBinding="{Binding Path=GpsSignal}"/>
</GridView>
</ListView.View>
</ListView>
和下面是code来实现结合
And the following is the code to implement the binding
//Constructor
public MainWindowViewModel()
{
DiscoveredDevicesList = new ObservableCollection<MyDeviceInfo>();
}
private ObservableCollection<MyDeviceInfo> _DiscoveredDevicesList;
public ObservableCollection<MyDeviceInfo> DiscoveredDevicesList
{
get
{
return _DiscoveredDevicesList;
}
set
{
_DiscoveredDevicesList = value;
OnPropertyChanged("DiscoveredDevicesList");
}
}
使用添加()
和清除()
更新视图如下使用时就好了:
Using Add()
and Clear()
updates the view just fine when used as below:
var client = new BluetoothClient();
DiscoveredDevicesList.Clear();
IEnumerable<MyDeviceInfo> csaDevices = null;
csaDevices = await DiscoverCsaDevicesAsync(client);
foreach (var csaDevice in csaDevices)
{
DiscoveredDevicesList.Add(csaDevice);
DiscoveredDevicesList.First().GpsSignal = true;
}
所以,我可以看到从在我看来,最初的对面更改为 TURE
的 GpsSignal
的值。
不过,如果我把下面的同一行中的的OnClick
按钮不会做同样的和值保持的假
,因为我没有使用任何添加()
或清除()
和我单纯依靠 OnPropertyChanged
这样的伎俩对我来说。
However, if I put the following same line in the OnClick
of a button doesn't do the same and the value stays false
since I am not using any Add()
or Clear()
and I simply rely on the OnPropertyChanged
to do the trick for me.
DiscoveredDevicesList.First().GpsSignal = true;
绑定,如按钮点击和文本块信息做工精细的休息,那么我认为它不应该与实施在舞台背面的结合问题。
The rest of the bindings such as button clicks and textblock information work fine, then I would assume it shouldn't be the problem with implementation of the binding on the back of the stage.
应该AP preciate你对这个建议。
Would appreciate your suggestions on this.
推荐答案
如果您MyDeviceInfo类实现INotifyPropertyChanged和你的财产GpsSignal提高OnPropertyChanged(),你应该看到所有的变化,当你绑定是正确的。
if your MyDeviceInfo class implement INotifyPropertyChanged and your property GpsSignal raise OnPropertyChanged() you should see all changes when your bindings are right.
不过我会改变你的code是这样的:
nevertheless i would change your code in this way:
从资源删除CollectionViewSource和使用简单的结合
remove CollectionViewSource from your resources and use simple binding
<Window.Resources>
</Window.Resources>
<ListView ItemsSource="{Binding DeviceList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Device name" DisplayMemberBinding="{Binding Path=DeviceName}"/>
<GridViewColumn Header="Rssi" DisplayMemberBinding="{Binding Path=Rssi}"/>
<GridViewColumn Header="GPS Row" DisplayMemberBinding="{Binding Path=GpsSignal}"/>
</GridView>
</ListView.View>
</ListView>
为你的窗口在DataContext设置为codebehind或XAML的MainWindowViewModel。
而不是使用的OnClick,请用个ICommand(RelayCommand或DelegateCommand)
set the datacontext for your window to your MainWindowViewModel in codebehind or xaml. instead of using OnClick, pls use ICommands (RelayCommand or DelegateCommand)
<Button Command="{Binding MyCommand4GpsSignalIsTrue}" />
这篇关于列表视图绑定不更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!