none
Label.Content Changed Event RRS feed

  • Question

  • I have a label "NetStatus". I need to trigger a procedure code if NetStatus.Content has changed.

    I was looking something similar to TextChanged event.

    How do I know if the content has changed or not?

    Gautham

    Sunday, March 9, 2008 8:01 PM

Answers

  • The most dynamic approach is to simply derive your own label control that provides a ContentChanged event:

     

    Code Snippet

     

    public class MyLabel : Label

    {

        static MyLabel()

        {

            ContentProperty.OverrideMetadata(typeof(MyLabel),

                new FrameworkPropertyMetadata(

                    new PropertyChangedCallback(OnContentChanged)));

        }

     

        private static void OnContentChanged(DependencyObject d,

            DependencyPropertyChangedEventArgs e)

        {

            MyLabel mcc = d as MyLabel;

            if (mcc.ContentChanged != null)

            {

                DependencyPropertyChangedEventArgs args

                    = new DependencyPropertyChangedEventArgs(

                        ContentProperty, e.OldValue, e.NewValue);

                mcc.ContentChanged(mcc, args);

            }

        }

     

        public event DependencyPropertyChangedEventHandler ContentChanged;

    }

     

     

    The markup would look like this:

     

    Code Snippet

     

    <src:MyLabel ContentChanged="cc_ContentChanged" Content="{Binding whatever}" />

     

     

     

    where src is a namespace mapping to whatever namespace contains your MyLabel control.
    Sunday, March 9, 2008 9:27 PM

All replies

  • Since the Label is a ContentControl and can contain other controls (not just text), the answer to your question would depend upon what the content of your Label actually is.

     

    Hope this helps,

     

    Keith

     

     

     

    Sunday, March 9, 2008 8:32 PM
  • In my case NetStatus.Content just contains text.
    Sunday, March 9, 2008 8:37 PM
  • I'm not sure, but I think the would want to handle the PreviewTextInput input event of the Label. I haven't tested this, but I am guessing it is what you are looking for.

     

    Hope this helps,

     

    Keith

     

    Sunday, March 9, 2008 8:55 PM
  • Nope PreviewTextInput is not triggered. I'm actually using a C4F P2P NetworkManager. I've linked the NetworkManager's Status Control to NetStatus. I need to know the status of the connection. So I need a handle on NetStatus when the content(text) changes.

    Sunday, March 9, 2008 9:04 PM
  • Perhaps you can you capture the TargetUpdated event on your binding.

     

    Hope this helps,

     

    Keith

     

    Sunday, March 9, 2008 9:16 PM
  • The most dynamic approach is to simply derive your own label control that provides a ContentChanged event:

     

    Code Snippet

     

    public class MyLabel : Label

    {

        static MyLabel()

        {

            ContentProperty.OverrideMetadata(typeof(MyLabel),

                new FrameworkPropertyMetadata(

                    new PropertyChangedCallback(OnContentChanged)));

        }

     

        private static void OnContentChanged(DependencyObject d,

            DependencyPropertyChangedEventArgs e)

        {

            MyLabel mcc = d as MyLabel;

            if (mcc.ContentChanged != null)

            {

                DependencyPropertyChangedEventArgs args

                    = new DependencyPropertyChangedEventArgs(

                        ContentProperty, e.OldValue, e.NewValue);

                mcc.ContentChanged(mcc, args);

            }

        }

     

        public event DependencyPropertyChangedEventHandler ContentChanged;

    }

     

     

    The markup would look like this:

     

    Code Snippet

     

    <src:MyLabel ContentChanged="cc_ContentChanged" Content="{Binding whatever}" />

     

     

     

    where src is a namespace mapping to whatever namespace contains your MyLabel control.
    Sunday, March 9, 2008 9:27 PM
  • This is how the NetworkManager is Declared:

    <my2:NetworkManager Margin="0,0,-211,0" Name="NetworkManager" xmlns:my2="clr-namespace:C4F.VistaP2P.WPF.Network;assembly=C4F_P2PWPFControls" Height="170" HorizontalAlignment="Right" VerticalAlignment="Top" Width="192" Chat="{Binding ElementName=ChatControl}" UserName="{Binding ElementName=NickName}" NetworkName="{Binding ElementName=Network}" Password="{Binding ElementName=Password}" SignInButton="{Binding ElementName=SignIn}" StatusValue="{Binding ElementName=NetStatus}"></my2:NetworkManager>

    This is what I've tried:

      Private Sub NetStatus_TargetUpdated(ByVal sender As Object, ByVal e As System.Windows.Data.DataTransferEventArgs) Handles NetStatus.TargetUpdated
      MsgBox("")
      End Sub

    Even I feel this must be working. But can't identify the problem.

    Sunday, March 9, 2008 9:28 PM
  •  Dr. WPF wrote:

    The most dynamic approach is to simply derive your own label control that provides a ContentChanged event:

    I would have done that. But The C4F P2P only binds to a System.Windows.Control.Label Control.

    Else if it was possible, I would have binded it to a TextBox which has a TextChanged Event.

    Sunday, March 9, 2008 9:33 PM
  • Try:

     

    StatusValue="{Binding ElementName=NetStatus, NotifyOnTargetUpdated=True}"

     

    Sunday, March 9, 2008 9:33 PM
  • You can either use the custom label that I posted above, or if StatusValue is a dependency property, you can use the same approach to provide a StatusValueChanged event.

     

    You could also create an arbitrary binding to NetStatus and bind it to your own DP.  Then use the property changed event of your DP to determine when the status changes.

     

    Note that all solutions require the NetStatus property to be observable... which means the object to which it belongs must provide a change notification when NetStatus changes (either by implementing INotifyPropertyChanged or by exposing NetStatus as a DP).

     

    Sunday, March 9, 2008 9:36 PM
  •  Gautham Gupta wrote:

    I would have done that. But The C4F P2P only binds to a System.Windows.Control.Label Control.

    Else if it was possible, I would have binded it to a TextBox which has a TextChanged Event.

     

    The control I gave you is a System.Windows.Control.Label control.

    Sunday, March 9, 2008 9:38 PM
  • The control you gave actually did the trick.

    But I don't understand how the custom control can be taken as a 'System.Windows.Control.Label'. Wouldn't it be a 'CustomNamespace.MyLabel'?

    Anyways, thanks for all the help Dr. WPF and Keith. You have actually saved a huge headache I had.

    Sunday, March 9, 2008 10:23 PM