我有一個按鈕和一個數據網格的視圖。 XAML文件是這樣的:僅第一次更新DataGrid綁定
<Window x:Class="MusicaDB.Views.PrincipalView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="Principal" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="627" d:DesignWidth="1176" SizeToContent="WidthAndHeight">
<Window.Resources>
<DataTemplate x:Key="BlueHeader">
<StackPanel Orientation="Horizontal" Margin="-5,-5,-5,-5" Width="120">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF223B84" Offset="1"/>
<GradientStop Color="#FF57A0F4" Offset="0.5"/>
<GradientStop Color="#FF4B94EC" Offset="0.5"/>
</LinearGradientBrush>
</StackPanel.Background>
<TextBlock Margin="10,10,10,10" Text="{Binding}" VerticalAlignment="Center" Foreground="White"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Button Content="Search"
Height="23"
HorizontalAlignment="Left"
Margin="12,165,0,0"
Name="btnPersonSearch"
VerticalAlignment="Top"
Width="48">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction
Command="{Binding PersonSearch}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Grid Height="558" Width="1099">
<DataGrid
Height="164"
HorizontalAlignment="Left"
Margin="339,24,0,0"
Name="dgdAutores"
VerticalAlignment="Top"
Width="540"
IsTextSearchEnabled="True"
CanUserAddRows="True"
CanUserDeleteRows="True"
ItemsSource="{Binding DgdPersons}">
<DataGrid.Columns>
<DataGridTextColumn Header="IDPerson" Binding="{Binding IDPerson}"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
在我ViewModel.cs我有兩個屬性,一個是綁定和其他的命令。
`
private ObservableCollection<Persons> _dgdPersons;
public ICommand _personsSearch { get; set; }
public ObservableCollection<Persons> DgdPersons
{
get { return _dgdPersons; }
set
{
_dgdPersons = value;
base.RaisePropertyChangedEvent("DgdPersons");
}
}
public ICommand PersonsSearch
{
get{return _personsSearch;}
}`
這是我用來搜索者的代碼,我在其中更新者:
using System;
using System.Windows.Input;
using PersonsDB.ViewModel;
using System.Collections.ObjectModel;
using PersonsDB.DBModels;
using System.Windows.Controls;
namespace MusicaDB.Commands
{
public class AutoresBuscarCommand : ICommand
{
private ViewModelMain _ViewModel;
public PersonsSearchCommand(ViewModelMain viewModel)
{
_ViewModel = viewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(Object parameter)
{
_ViewModel.DgdPersons = _ViewModel.DBManager.getPersons("Parameters of serach");
}
}
}
注:DBManager是一個類,讓我對數據庫的訪問。此類內部使用EF 4.1,方法getPersons返回context.Persons.Local。
這Command類從基類繼承:
using System.ComponentModel;
namespace PersonsDB.ViewModel
{
public abstract class ViewModelBase : INotifyPropertyChanging, INotifyPropertyChanged
{
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
public virtual bool IgnorePropertyChangeEvents { get; set; }
public virtual void RaisePropertyChangedEvent(string propertyName)
{
// Exit if changes ignored
if (IgnorePropertyChangeEvents) return;
// Exit if no subscribers
if (PropertyChanged == null) return;
// Raise event
var e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
public virtual void RaisePropertyChangingEvent(string propertyName)
{
// Exit if changes ignored
if (IgnorePropertyChangeEvents) return;
// Exit if no subscribers
if (PropertyChanging == null) return;
// Raise event
var e = new PropertyChangingEventArgs(propertyName);
PropertyChanging(this, e);
}
}
}
嗯,不要把大量的代碼,我有其他類,PersonsSearchCommand時執行的命令。該命令還使用其他類從DataBase請求數據。該類使用EF 4.1並返回context.Persons.Local。這個返回是我設置ModelView的ObservableCollection Persons的。
這起作用,至少和第一次點擊按鈕。 dataGrid不接收數據的其餘時間不會綁定新的結果。
我嘗試在我的命令類中首先使用clear()方法清除ObservableCollection人員,創建時間ObservableCollection以接收返回Persons.Local並使用具有此臨時ObservableCollection的foreach將其元素添加到ObservableCollection人員檢測到新的更改,但這不起作用。我知道這是一個糟糕的解決方案,因爲如果本地有很多元素,它可能會非常慢,但它是看我是否更新dataGrid,但不是。
爲什麼只在第一次使用?
謝謝。 Daimroc。
編輯:我已經添加了新的代碼。
編輯2:解決。
那麼,最後我發現了錯誤。問題不在於綁定,這是我的錯,因爲我已經將CodeBehind中的代碼轉換爲MVVM,並且我正在修改代碼隱藏中的ItemsSorce。
我沒有意識到,清除搜索條件的按鈕從代碼隱藏中更改了dataGrid,然後itemsSource以不可預料的方式進行了更改。
我現在已經創建了一個新的按鈕清理命令,它的工作原理應該如此。
非常感謝您的幫助。 Daimroc。
它可能只是我,但我沒有看到你的XAML中的按鈕。你可以發佈實際更新'DgdPersons'的代碼嗎? – Rachel
您應該閱讀:http://sscce.org/ – Will