如何更改Silverlight DataContext对象中的数据

时间:2012-10-25 19:05:51

标签: silverlight

我有一个datacontext对象,显示上传文件列表和副本数量。到目前为止,用户可以单击每行中的编辑图标,然后检索该行中的数据,但我不知道如何更改任何数据。

以下是主页上“更改详情”对话框的按钮事件:

    private void Image_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
    {
        Image b = sender as Image;
        FileReceivedItem fi = b.DataContext as FileReceivedItem;
        string fileCopies = fi.fileCopies;
        string fileName = fi.FileName;
        DetailsWindow detailsDlg = new DetailsWindow(fileCopies, fileName);
        detailsDlg.Show();
        detailsDlg.Closed += new EventHandler(detailsWnd_Closed);
    }

    public DetailsWindow detailsDlg;
    void detailsWnd_Closed(object sender, EventArgs e)
    {
        MessageBox.Show("I want to change fileCopies here");
    }

XAML的相关部分:

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden"  ItemsSource="{StaticResource FileReceivedItem}"  Name="filesReceivedList" HorizontalAlignment="Left"  Grid.Row="1" Grid.Column="1"  Background="White" Margin="12,23,0,0"
     VerticalAlignment="Top" Width="411" Height="175" Loaded="filesReceivedList_Loaded">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Width="404" Height="19" Orientation="Horizontal" >
                    <TextBlock x:Name="ListFileName" Text="{Binding FileName}" Width="283"  VerticalAlignment="Center">
                        <ToolTipService.ToolTip>
                            <ToolTip Content="{Binding FileName}"></ToolTip>
                        </ToolTipService.ToolTip>
                    </TextBlock>
                    <TextBlock Text="{Binding fileCopies}" Width="50"  VerticalAlignment="Center"/>
                    <TextBlock Text="{Binding FileID}" Width="0" Visibility="Collapsed"  VerticalAlignment="Center"/>

                    <Image Source="images/Edit.png" MouseLeftButtonUp="Image_MouseLeftButtonUp_1" Width="20" Margin="0 0 0 0">
                        <ToolTipService.ToolTip>
                            <ToolTip Content="Edit"></ToolTip>
                        </ToolTipService.ToolTip>
                    </Image>

                    <TextBlock Text="" Width="10" />

                    <Image Source="images/Delete.png" MouseLeftButtonUp="Image_MouseLeftButtonUp" >
                        <ToolTipService.ToolTip>
                            <ToolTip Content="Delete"></ToolTip>
                        </ToolTipService.ToolTip>
                    </Image>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

如果缺少任何重要的代码,请告诉我。我不确定数据源是如何工作的。

1 个答案:

答案 0 :(得分:0)

我最终得到了它的工作。非常直截了当。我只是没有在ahwile中使用过SL。这就是我所做的,尽管可能有更好的方式可以让某人提供更好的答案:

    void detailsWnd_Closed(object sender, EventArgs e)
    {
        changeFileCopies(detailsDlg.FileName, detailsDlg.nCopies.Text);
    }

    public void changeFileCopies(string filename, string newCopies)
    {
        var row = (FileReceivedItem)filesReceivedDataSource.Where(x => x.FileName == filename).FirstOrDefault();
        row.fileCopies = newCopies;
        filesReceivedList.ItemsSource = null;
        this.filesReceivedList.ItemsSource = filesReceivedDataSource;
    }