WPF DataGrid SelectedItems不适用于生产

时间:2014-02-26 19:43:13

标签: wpf

WPF代码

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfApplication1" xmlns:data="clr-namespace:System.Data;assembly=System.Data"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary>
            <my:tst x:Key="tst" />
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="grdDataTable"
                        CanUserAddRows="False" 
                        CanUserReorderColumns="False" 
                        Grid.Row="1"
                        SelectionMode="Single"
                        IsReadOnly="True"
                        CanUserResizeColumns="False" 
                        Margin="10" AutoGenerateColumns="True" 
                        SelectedItem="{Binding Path=SelectedRow,UpdateSourceTrigger=PropertyChanged}"
                        ItemsSource="{Binding Path=DtCollection, UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">

            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Row.RowState}" Value="4">
                            <Setter Property="DataGridCell.Background" Value="Aqua"></Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Row.RowState}" Value="16">
                            <Setter Property="DataGridCell.Background" Value="AntiqueWhite" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.RowDetailsTemplate>

                <DataTemplate>
                    <DataGrid x:Name="InnerGrid" Height="80"  HeadersVisibility="Row" 
                              DataContext="{Binding Path=ItemsSource, ElementName=grdDataTable}"
                                         Background="DarkGray" 
                                          CanUserAddRows="False" 
                                          CanUserReorderColumns="False" 
                                          CanUserResizeColumns="False" 
                                          CanUserDeleteRows="False" 
                                          CanUserResizeRows="False" 
                                          CanUserSortColumns="False"
                                          Margin="-8,5,0,0" 
                                          AutoGenerateColumns="True" 
                                          SelectedItem="{Binding Path=SelectedRow,UpdateSourceTrigger=PropertyChanged}"
                                          Tag="{Binding Path=ItemsSource, ElementName=grdDataTable}">
                        <DataGrid.ItemsSource>
                            <Binding ElementName="grdDataTable" Path="SelectedItems"                                      
                                     UpdateSourceTrigger="PropertyChanged"  />
                            </DataGrid.ItemsSource>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</Window>

背后的代码

namespace WpfApplication1 {
    using System.ComponentModel;
    using System.Data;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged 
    {


        private DataTable dtCollection=new DataTable();
        public DataTable DtCollection
        {
            get
            {
                return this.dtCollection;
            }
            set
            {
                this.dtCollection = value;
                OnPropertyChanged("DtCollection");

            }
        }

        public MainWindow()
        {
            InitializeComponent();

            LoadTable();
            this.grdDataTable.DataContext = this;


        }

        private void LoadTable()
        {
            DtCollection.Columns.Add("Name", typeof(string));
            DtCollection.Columns.Add("Add1", typeof(string));

            DtCollection.Columns.Add("Add2", typeof(string));

            for (int i = 0; i < 10; i++)
            {
                DataRow dr = DtCollection.NewRow();
                dr[0] = i.ToString();
                dr[1] = (i * 10).ToString();
                dr[2] = (i * 100).ToString();
                DtCollection.Rows.Add(dr);
            }
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }
}

以上代码在开发机器上运行良好。我部署了 改变生产,但一旦我选择顶部网格上的行 在底部网格中看不到任何内容。我很有信心 一些绑定问题但无法在生产中安装任何软件 去检查。只有生产和发展之间的区别 开发有4.5版本的.net但生产有4.0。虽然我 在4.0下开发了我的代码。编辑以下数据网格中的值后,应立即更改父行值。在我的本地机器上一切正常。我在datatemplate中使用datagrid的原因是我需要进行所有验证,而我的cols是动态的。

今天我浪费了一整天的时间来解决这个问题,由于时间的限制,我们将立即回答。

1 个答案:

答案 0 :(得分:0)

我不敢告诉你,唯一能解决问题的人就是你。向我们展示您的代码无济于事,因为我们无法用它重现您的问题。像你这样的所有问题都可以用同样的方式解决。您基本上需要在程序运行时找出各种对象的值。您显然无法调试生产版本,因此您需要做更多工作才能找到值。

主要有两种方式......第一种也是最简单的方法是添加一大堆调用MessageBox.Show,提醒您注意各种对象的值。如果您可以在最终用户无权访问应用程序的情况下进行测试,则可以。如果他们这样做,那么您将需要添加某种日志记录,将值保存到数据库或文本文件中,您可以在运行应用程序后查看。

一旦你了解了这些价值观,你就能找出问题所在。很可能它与权限或文件路径不正确有关。