刷新WPF页面

时间:2012-02-07 18:54:47

标签: c# wpf

我有一个应用程序,我已编写该应用程序来查看已为该公司编写的自定义应用程序的日志。我使用实体模型进行设置,并希望根据菜单选择动态更改我要访问的数据库(测试数据库或生产数据库)。我尝试过使用EntityConnectionStringBuilder,它似乎没有错误,但是在更改数据连接字符串后,我无法获得显示要更新的应用程序列表的网格。 (测试和生产服务器的连接字符串存储在properties.settings中)以下是切换到测试服务器的内容

try
{
  XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
  var query1 = from p in doc.Descendants("connectionStrings").Descendants()
             select p;
  foreach (var child in query1)
  {
    foreach (var atr in child.Attributes())
    {
        if (atr.Name.LocalName == "name" && atr.Value == "AppsEntities")
        if (atr.NextAttribute != null && atr.NextAttribute.Name == "connectionString")
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value);
            entityBuilder.ProviderConnectionString = Properties.Settings.Default["TestServer"].ToString();
            atr.NextAttribute.Value = entityBuilder.ToString();
        }
    }
  }
  doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
  lblDatabase.Content = "Connected to the Test Server";
}
catch (Exception ex)
{
  MessageBox.Show("changing connection string to Test - blew up!" + ex.Message, "massive error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}

任何帮助都将不胜感激我是C#和WPF的新手,所以我可能不会以正确的方式解决这个问题。

 <telerik:RadGridView x:Name="LogApplicationGrid"
                         Width="900"
                         Height="Auto"
                         Margin="2,53,0,0"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         AutoGenerateColumns="false"
                         GridLinesVisibility="Horizontal"
                         IsReadOnly="true"
                         MouseDoubleClick="LogApplicationGrid_MouseDoubleClick"
                         ShowGroupPanel="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn MaxWidth="50"
                                        DataMemberBinding="{Binding AppID}"
                                        Header="AppID"
                                        IsFilterable="False"
                                        UniqueName="appNumber" />
            <telerik:GridViewDataColumn MaxWidth="300"
                                        DataMemberBinding="{Binding AppName}"
                                        Header="Application Name" />
            <telerik:GridViewDataColumn MaxWidth="350"
                                        DataMemberBinding="{Binding AppDesc}"
                                        Header="Application Description" />
            <telerik:GridViewDataColumn MaxWidth="150"
                                        DataMemberBinding="{Binding DateCreated}"
                                        Header="Date Created"
                                        IsFilterable="False" />
            <telerik:GridViewCheckBoxColumn MaxWidth="100"
                                            DataMemberBinding="{Binding ShowInSharePoint}"
                                            Header="SharePoint"
                                            IsFilterable="False" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

1 个答案:

答案 0 :(得分:1)

将GridView绑定到ObservableCollection<T>,然后更新网格数据,只需更新集合。

使用ObservableCollection<T>代替List<T>,因为它会自动提醒用户界面在收集更改时需要更新。

另外,请确保绑定值而不是设置它。设置值会将值设置为数据实例,而不是实际数据本身,因此它不会响应更改。

例如,这不会更新UI

LogApplicationGrid = SomeCollection;
SomeCollection = GetObservableCollection();

但这会

var b = new Binding();
b.Source = SomeCollection;
LogApplicationGrid.SetBinding(RadGridView.ItemsSourceProperty, b);

SomeCollection = GetObservableCollection();

这也将

<telerik:RadGridView ItemsSource="{Binding SomeCollection}" ... />