C# 定义方法WPF用户控件
我有一个用户控件: 我将此用户控件添加到我的Winforms应用程序simple BusyIndicator中C# 定义方法WPF用户控件,c#,.net,wpf,C#,.net,Wpf,我有一个用户控件: 我将此用户控件添加到我的Winforms应用程序simple BusyIndicator中 UserControl x:Class="Stackoverflow.MyBusyIndicator" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
UserControl x:Class="Stackoverflow.MyBusyIndicator"
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>
我要做的就是定义一个方法,我可以从c代码中访问这个方法来停止这个指示器。我相信你想在后面的代码中做什么
public partial class MyBusyIndicator : UserControl
{
public void ToggleIndicator(bool isBusy)
{
// Just an example, in reality you will want to access the BusyIndicator object.
this.IsBusy = isBusy;
}
}
试试这个:
<UserControl x:Class="Stackoverflow.MyBusyIndicator"
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="myBusyIndicator" IsBusy="True" />
</Grid>
</UserControl>
您的XAML代码很好,现在只需创建一个依赖项属性并将其称为IsBusy,然后可以使用数据绑定在UserControl XAML中绑定到它,以直观地指示IsBusy属性状态
public partial class BusyIndicator : UserControl
{
public BusyIndicator()
{
InitializeComponent();
}
public bool IsBusy
{
get { return (bool)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator),
new PropertyMetadata(false));
}
如果必须首先通过代码隐藏访问它,请首先通过Xaml向BusyIndicator控件提供name属性: 在代码隐藏中创建一个方法,如下所示:
void SetIndicator(bool isBusy)
{
this.busyIndicator.IsBusy = isBusy;
}
void SetIndicator(bool isBusy)
{
IsBusy = isBusy;
}
如果使用MVVM绑定控件的IsBusyProperty IsBusy={Binding IsBusy}
并在viewmodel中定义IsBusy属性和创建方法,如下所示:
void SetIndicator(bool isBusy)
{
this.busyIndicator.IsBusy = isBusy;
}
void SetIndicator(bool isBusy)
{
IsBusy = isBusy;
}
因此,下次您要将其设置为True时,请调用SetIndicatortrue,或者如果您要将其设置为false,请调用SetIndicatorfalse。。。现在从代码后面访问这个对象来控制它的行为…我试着把它放在我的USER控件的中间,并且存在错误,在什么地方我需要把它放在上面。我不熟悉WPFRATE只是替换上面的行……我不能从代码中访问这一点,我需要定义为什么我需要部分类MyBuyTealth: UserControl?我需要在哪里定义切换指示器?this.IsBusy不存在您已经有一个部分类MyBusyIndicator,我是从您的示例中获取的?后面的代码就是你声明方法的地方。我需要在哪里声明这个方法?在哪里声明这个类?这个类应该已经存在了吗?您是否创建了示例XAML?只需在VisualStudio中的MyBusyIndicator.xaml上单击鼠标右键,然后选择“查看代码”来修改现有的MyBusyIndicator.xaml.cs。我没有控制器名myBusyIndicator,我需要在其中放置公共部分类myBusyIndicator:UserControl?但我不明白我需要在解决方案中将此部分类myBusyIndicator:UserControl创建为新类的是什么?在本例中,分部类包含所有内部XAML编码,如上面示例中看到的InitializeComponent方法……现在我可以看到了,但在设置UserControl1 uc1=new UserControl1之后,这仍然继续;uc1.ToggleIndicatorfalse;-我是从主窗体中的代码中完成的。您必须使用Windows或窗体来将BusyIndicator父窗体添加到主窗体中。尝试用谷歌搜索你会得到所有信息…此行:有错误:错误2{'是一个意外的标记。预期的标记是''或''。我正在从主窗体使用它,我不知道什么是MVVMIsBusy={Binding IsBusy}如果你像我上一篇文章和其他海报中提到的那样通过主窗体或代码隐藏来使用它,那么你必须命名你的控件。这将允许你从代码隐藏访问控件的IsBusy属性。现在它停止了,但UserControl1 uc1=new UserControl1;uc1.SetIndicatortrue、 不会让这件事再次发生