how to add content changed event RRS feed

  • 问题

  • <!--labelstyle-->
            <Style x:Key="mylabelstyle" TargetType="Label">
                <Setter Property="Content" Value="0"></Setter>
                <Setter Property="Margin" Value="0.5"/>
                <EventSetter Event="MouseDown" Handler="myLabel_MouseDown"></EventSetter>
                    <Trigger Property="Content" Value="0">
                        <Setter Property="Background" Value="White"/>
                    <Trigger Property="Content" Value="1">
                        <Setter Property="Background" Value="Yellow"/>
     <StackPanel x:Name="Num1" MouseLeftButtonDown="row_update" Margin="10,0,0,2" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Center">
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit31" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit30" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit29" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit28" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit27" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit26" ></Label>
                        <Label Style="{StaticResource mylabelstyle}" x:Name="bit25" ></Label>
    how to add an event to observe  label 's content whether it is changed ? i want to add it in its style, any way please?
    2014年10月31日 8:01


  • Hi Rogerguo,

    Based on my research here is a solution which can help you create a content changed event:

    Please check the following thread:

    And use the following code from Dr. WPF  to create contentchanged event for your label control:

    public class MyLabel : Label
        static 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;

    For the  specific value before and after the property changed, you can have a look at the following page:

    Best regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 Rogerguo 2014年11月3日 12:01
    2014年11月3日 7:16