【问题标题】:How to start my BusyIndicator from code behind如何从后面的代码启动我的 BusyIndi​​cator
【发布时间】:2015-02-25 17:34:22
【问题描述】:

我有这个用户控件:我将此用户控件添加到我的 Winforms 应用程序(简单的 BusyIndi​​cator):

<UserControl x:Class="MyApp.UserControl1"             
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <xctk:BusyIndicator x:Name="busyIndicator" IsBusy="{Binding IsBusy}"  />
    </Grid>
</UserControl>



   public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public void SetIndicator(bool isBusy)
        {
            busyIndicator.IsBusy = isBusy;
        }
    }

现在我尝试从我的主要表单开始我的BusyIndocator:

UserControl1 uc = new UserControl1();
uc.SetIndicator(true);

另外,如果我将 IsBusy="{Binding IsBusy}" 更改为 IsBusy="true,我可以看到我的 BusyIndi​​cator,但我无法停止它。

但是什么也没发生,我做错了什么?

【问题讨论】:

  • 1)这是winforms还是wpf?应该是后者,但是为什么winforms标签呢? 2)你在datacontext中绑定到IsBusy到IsBusy,那你为什么要在SetIndicator方法中手动设置IsBusy属性呢? 3)你在哪里设置数据上下文?
  • 1) 这是winforms 2) 我设置了busyIndi​​cator.IsBusy = isBusy;在 SetIndicator 函数 3) 你是什么意思 datacontext ?
  • winforms从什么时候开始使用xaml?
  • 我在询问后添加了 wpf usercontrol 并告诉我它可能
  • 1)这不可能是winforms,你确定吗?你在使用 winforms/wpf 互操作吗? 2)为什么在使用 DataBinding 时设置busyIndicator.IsBusy ? 3)我认为你对你使用的技术有错误的理解。请先学习wpf here

标签: c# wpf winforms busyindicator


【解决方案1】:

不熟悉WPF,请勿使用数据绑定,使用代码设置IsBusy的值。

<xctk:BusyIndicator x:Name="busyIndicator" /> //remove IsBusy="{Binding IsBusy}"

在您的表单中,定义一个UserControl1 的实例并为其命名为uc;

public partial class MyForm : Form
{        
    UserControl1 uc; //this is the "instance".
    public MyForm ()
    {
        InitializeComponent();
        uc = new UserControl1();
        uc.SetIndicator(true); //start the busy indicator
        this.elementHost1.Child = uc;
    }
}

如果你想阻止它,请致电

uc.SetIndicator(false); //stop the busy indicator

【讨论】:

  • @kennuzx 你能分享你的SetIndicator() 方法吗?我不知道该怎么做,在用户控件的代码中,IsBusy 属性不可访问。
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 2011-04-02
  • 2012-11-28
相关资源
最近更新 更多