【问题标题】:How to change data in a Silverlight DataContext Object如何更改 Silverlight DataContext 对象中的数据
【发布时间】:2012-10-25 19:05:51
【问题描述】:

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

这是主页上“更改详细信息”对话框的按钮事件:

    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>

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

【问题讨论】:

  • 将您的编辑移至答案并回答您自己的问题。这样人们就可以对其进行投票并发现它更容易。

标签: silverlight


【解决方案1】:

我最终让它工作了。很简单。我只是没有在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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2010-10-11
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多