加载usercontrol并显示WPF后执行某些操作

时间:2014-08-27 09:47:36

标签: c# wpf user-controls

我有一个UserControl,我正在ContentControl 点击Button动态加载它。我在TextBlock中有一个UserControl,我希望在TextBlock之后UserControl动态显示一些文本(这基本上是我的另一个方法将在处理请求时返回的状态)已加载{1}}。我尝试在Loaded的{​​{1}} 事件中进行设置,但在UserControl完全加载并显示时,文字已经存在。

有人可以就如何实现这一点提出一个想法。我查看了thisthis链接,但似乎没有一个对我有用。

谢谢Deepak

1 个答案:

答案 0 :(得分: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内容可以模拟数据的加载。

最后,这是反向bool到可见性转换器:

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();
        }
    }
}