2014-08-27 37 views
1

我有UserControl,我正在ContentControl上動態加載它Button點擊。我在UserControl中有一個TextBlock,我想在加載UserControl之後在TextBlock中動態顯示一些文本(基本上是我的另一種方法在處理請求時將返回的狀態)。我試圖在UserControlLoaded事件中設置此項,但當UserControl完全加載並顯示時,文本已經存在。在用戶控件加載並顯示後執行某些操作WPF

有人可以提供一個關於如何實現這一點的想法。我檢查了thisthis鏈接,但似乎沒有人爲我工作。

感謝迪帕克

回答

0

如果你的負載是邏輯而不是物理你必須自己處理。

特別是你必須有一種方法來告訴UserControl何時加載數據。

這裏使用MVVM(不完美)全樣本:

UserControl

<UserControl x:Class="WpfApplication1.LoadDataView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApplication1" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <BooleanToVisibilityConverter x:Key="boolToVizConverter"></BooleanToVisibilityConverter> 
     <local:ReverseBooleanToVisibilityConverter x:Key="reverseBoolToVizConverter"></local:ReverseBooleanToVisibilityConverter> 
    </UserControl.Resources> 
    <Grid> 
     <TextBlock Visibility="{Binding Model.IsLoadingData,Converter={StaticResource boolToVizConverter}}">...</TextBlock> 
     <TextBlock Visibility="{Binding Model.IsLoadingData,Converter={StaticResource reverseBoolToVizConverter}}">Data loaded</TextBlock> 
    </Grid> 
</UserControl> 

所以"..."顯示當我們加載(在實際應用中,你會用更好的UI控件來呈現此狀態,如ProgressRing)。

當數據加載時,我們顯示"Data loaded"

後面的代碼:

using System.Windows; 
using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    public partial class LoadDataView : UserControl 
    { 
     public LoadDataViewModel Model 
     { 
      get { return (LoadDataViewModel)GetValue(ModelProperty); } 
      set { SetValue(ModelProperty, value); } 
     } 

     public static readonly DependencyProperty ModelProperty = DependencyProperty.Register("Model", typeof(LoadDataViewModel), typeof(LoadDataView)); 

     public LoadDataView() 
     { 
      InitializeComponent(); 

      this.DataContext = this; 
     } 
    } 
} 

視圖模型:

using System.ComponentModel; 

namespace WpfApplication1 
{ 
    public class LoadDataViewModel : INotifyPropertyChanged 
    { 
     private bool isLoadingData = false; 
     public bool IsLoadingData 
     { 
      get { return isLoadingData; } 
      set 
      { 
       if (value != isLoadingData) 
       { 
        isLoadingData = value; 
        PropertyChanged(this, new PropertyChangedEventArgs("IsLoadingData")); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
    } 
} 

以下是主要觀點:

<StackPanel> 
    <ContentControl x:Name="content"></ContentControl> 
    <Button Click="Button_Click">Display data</Button> 
</StackPanel> 

當我們點擊按鈕,我們顯示的數據UserControl我們觸發加載:

private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    LoadDataViewModel model = new LoadDataViewModel { IsLoadingData = true }; 

    content.Content = new LoadDataView { Model = model }; 

    await Task.Delay(3000); 

    model.IsLoadingData = false; 
} 

async/awaitTask東西只是在那裏模擬數據的加載。

最後這裏是相反的布爾能見度轉換器:

using System; 
using System.Windows; 
using System.Windows.Data; 

namespace WpfApplication1 
{ 
    public class ReverseBooleanToVisibilityConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      return !(bool)value ? Visibility.Visible : Visibility.Collapsed; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 
相關問題