0

我有这个用户控件:我将此用户控件添加到我的 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,但我无法阻止它。

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

4

1 回答 1

0

不要使用数据绑定,因为您不熟悉 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
于 2014-12-29T15:16:17.330 回答