简体   繁体   English

刷新WPF页面

[英]Refresh a WPF Page

I have an application that I have written to view logs from custom applications that have been written for this company. 我有一个编写的应用程序,可以查看为该公司编写的自定义应用程序中的日志。 I have it set up using an Entity Model and would like to dynamically change which database I am hitting (test database or production database) based on a menu selection. 我使用实体模型进行设置,并希望根据菜单选择动态更改我要访问的数据库(测试数据库或生产数据库)。 I have tried using an EntityConnectionStringBuilder and it appears to work with no errors, however I can not get the grid that displays the list of applications to update after changing the data connection string. 我尝试使用EntityConnectionStringBuilder,它似乎可以正常工作,但是更改数据连接字符串后,无法获得显示要更新的应用程序列表的网格。 (the connection strings for test and production servers are stored in the properties.settings) Here is what I have for switching to the test server (测试服务器和生产服务器的连接字符串存储在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);
}

Any help would be appreciated I am new to C# and WPF's so I may not be going about this the correct way. 任何帮助将不胜感激,因为我是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>

Bind your GridView to an ObservableCollection<T> , then to update your Grid's data, simply update the collection. 将GridView绑定到ObservableCollection<T> ,然后要更新网格的数据,只需更新集合即可。

Use an ObservableCollection<T> instead of a List<T> because it will automatically alert the UI that it needs to be updated when the collection changes. 使用ObservableCollection<T>而不是List<T>因为它会在集合更改时自动提醒UI,需要对其进行更新。

Also, be sure you're binding the value instead of setting it. 另外,请确保您绑定的是值而不是设置它。 Setting the value will set the value to an instance of the data, not the actual data itself, so it won't respond to changes. 设置值会将值设置为数据实例,而不是实际数据本身,因此它不会响应更改。

For example, this will not update the UI 例如,这不会更新用户界面

LogApplicationGrid = SomeCollection;
SomeCollection = GetObservableCollection();

But this will 但这会

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

SomeCollection = GetObservableCollection();

and this also will 这也将

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM