简体   繁体   中英

Define method WPF user control

I have this User control: I added this user control to my Winforms application (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"
         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>

And all i want is define Method that i can access from c# code in order to stop this indicator.

I believe what you want to do this in the code behind?

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

Try this :

<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>

Code Bahind :

namespace Stackoverflow
{
   using System;
   using System.Windows;
   using System.Windows.Controls;

   public partial class MyBusyIndicator : UserControl
   {
      public MyBusyIndicator()
      {
         this.InitializeComponent();
      }
      public void ShowIndicator(bool isBusy)
      {
         this.myBusyIndicator.IsBusy = isBusy;
      }
    }
}

Your XAML code is fine, now just create a Dependency Property and call it "IsBusy", which you then can bind to in your UserControl XAML using a DataBinding (to visually indicate the IsBusy property state),

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

If you have to access it through code-behind first provide a name attribute to the BusyIndicator control through Xaml first: <xctk:BusyIndicator IsBusy="True" x:Name="busyIndicator" />

In your code behind create a method as below:

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

If you are using MVVM bind your control's IsBusyProperty IsBusy={Binding IsBusy}

<xctk:BusyIndicator IsBusy={Binding IsBusy} />

And in your viewmodel define IsBusy property and create method as below:

void SetIndicator(bool isBusy)
{
    IsBusy = isBusy;
}

So next time you want to set is as True call SetIndicator(true) or if you want to set it as false call SetIndicator(false).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM