none
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>
                <Style.Triggers>
                    <Trigger Property="Content" Value="0">
                        <Setter Property="Background" Value="White"/>
                    </Trigger>
                    <Trigger Property="Content" Value="1">
                        <Setter Property="Background" Value="Yellow"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    
    
     <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>
    </StackPanel>
    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:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/5421e988-f729-4154-b1ab-e17e1b8b70d4/labelcontent-changed-event?forum=wpf

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

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

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

    http://msdn.microsoft.com/en-us/library/System.Windows.DependencyPropertyChangedEventArgs_properties(v=vs.110).aspx

    Best regards,



    Barry
    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
    版主