ItemControl绑定数据没有效果
在XAML界面上我使用一个ItemControl作为数据容器,然后在它的数据模板里使用了3个控件,代码如下:
<Style x:Key="GuestTilePanelStyle" TargetType="ItemsControl">
<Setter Property="xc:xTilePanel.ItemHeight" Value="30" />
<Setter Property="xc:xTilePanel.ItemWidth" Value="120" />
<Setter Property="ItemsControl.ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<xc:xTilePanel AnimatesNewItem="True" Attraction="2.0" Dampening="0.40" Variation="1.00" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsControl.ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid x:Name="gProgressContainer" Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<xc:xSprocket x:Name="skProgress" Grid.Column="0" Width="30" Height="30" TickColor="#FF02AA1A" />
<xc:xShadePanel x:Name="spInfo" Grid.Column="1" HorizontalContentAlignment="Center" Transition="UpTransition">
<TextBlock Text="{Binding name}" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center" />
</xc:xShadePanel>
<xc:xIcoButton x:Name="btnCancel" Grid.Column="2" Width="20" xIconSize="16" xIcon="/Takewin.OS.Modularity.FePlat;component/Resources/i0001000000210003.png" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<ItemsControl x:Name="itcSyncGuest" Grid.Column="0" Margin="0,5,0,0" Width="150" Grid.Row="3" ItemsSource="{Binding}" Style="{StaticResource ResourceKey=GuestTilePanelStyle}" />
private class GuestData
{
public int mid;
public string name;
}
private ObservableCollection<GuestData> _Source;
在load事件里我初始化并绑定数据源
this._Source = new ObservableCollection<GuestData>();
this.itcSyncGuest.ItemsSource = this._Source;
private void SyncGuestToSource(int meetId)
{
GuestData data = new GuestData() { mid = meetId };
this._Source.Add(data);
BackgroundWorker bg = new BackgroundWorker();
bg.WorkerReportsProgress = true;
bg.WorkerSupportsCancellation = true;
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
bg.RunWorkerAsync(data);
}
private void bg_DoWork(object sender, DoWorkEventArgs e)
{
//在数据源中的索引位置
GuestData data = (GuestData)e.Argument;
int index = this._Source.IndexOf(data);
e.Result = index;
string sql = string.Format("select * from TGuest where KMeetId={0}", data.mid);
DataTable dtData = this._Dao.QueryDataSet(sql).Tables[0];
if (dtData != null && dtData.Rows.Count > 0)
{
foreach (DataRow dr in dtData.Rows)
{
data.name = Convert.ToString(dr["FName"]);
((BackgroundWorker)sender).ReportProgress(index, data);
}
}
}
private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int index = e.ProgressPercentage;
GuestData data = (GuestData)e.UserState;
this._Source[index] = data;
}
public class ViewModelClass : INotifyPropertyChanged
{
private string _name;
public string Name
{
get{return _name;}
}
.....
public void ChangeName(string newName)
{
_name=newName;
OnPropertyChanged("Name");
}
......
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChaned(string propName)
{
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
}