2009-03-31 35 views
15

Có cách nào để phát hiện thay đổi trong thuộc tính Văn bản của phần tử TextBlock sử dụng sự kiện không?Làm cách nào để phát hiện thay đổi trong thuộc tính Văn bản của một TextBlock?

(Tôi đang cố gắng để cung cấp một hình ảnh động để làm nổi bật các TextBlocks có Văn bản tài sản thay đổi trong một DataGrid)

+0

Hãy xem này: http://stackoverflow.com/a/2964694/2550529 – SepehrM

Trả lời

3

Theo như tôi có thể hiểu được không có bất kỳ sự kiện TextChanged trong TextBlock. Nhìn vào yêu cầu của bạn, tôi cảm thấy rằng tái tạo khuôn mẫu một hộp văn bản cũng sẽ không phải là một giải pháp khả thi. Từ tìm kiếm sơ bộ của tôi xung quanh, this có vẻ là một giải pháp khả thi.

+0

Đó chính xác là những gì tôi muốn làm. Cảm ơn nhiều. –

7

Đây là từ liên kết trong câu trả lời bioskope, nhưng Giản thể.

<TextBlock Text="{Binding YourTextProperty, NotifyOnTargetUpdated=True}" 
      TargetUpdated="YourTextEventHandler"/> 
+1

Bằng cách sử dụng mã đằng sau sự kiện, theo ý kiến ​​của tôi, đây phải là câu trả lời đúng. –

1

Bind các văn bản tài sản cho một thuộc tính phụ thuộc, trong đó có một kích hoạt sự kiện:

public static string GetTextBoxText(DependencyObject obj) 
{ 
    return (string)obj.GetValue(TextBoxTextProperty); 
} 

public static void SetTextBoxText(DependencyObject obj, string value) 
{ 
    obj.SetValue(TextBoxTextProperty, value); 
} 

public static readonly DependencyProperty TextBoxTextProperty = 
    DependencyProperty.RegisterAttached(
    "TextBoxText", 
    typeof(string), 
    typeof(TextBlockToolTipBehavior), 
    new FrameworkPropertyMetadata(string.Empty, TextBoxTextChangedCallback) 
    ); 

private static void TextBoxTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    TextBlock textBlock = d as TextBlock; 
    HandleTextChange(textBlock); 
} 

Trong XAML Bind đến tài sản văn bản TextBlock:

<TextBlock 
Text="{Binding SomeProperty, UpdateSourceTrigger=PropertyChanged}" 
    th:TextBlockBehavior.TextBoxText="{Binding Text, 
    RelativeSource={RelativeSource Self}}" /> 
0

Dưới đây là một cái gì đó bạn có thể sử dụng Tôi đã chọn từ Jerry Nixon và Daren May tại Học viện ảo của Microsoft "Developing Universal Windows Apps with C# and XAML" và mã chứa logic DependencyObject ở đây "(W8.1-WP8.1) UNIVERSAL APP FOR MVA".

namespace App1.Behaviors 
{ 
// <summary> 
/// Helper class that allows you to monitor a property corresponding to a dependency property 
/// on some object for changes and have an event raised from 
/// the instance of this helper that you can handle. 
/// Usage: Construct an instance, passing in the object and the name of the normal .NET property that 
/// wraps a DependencyProperty, then subscribe to the PropertyChanged event on this helper instance. 
/// Your subscriber will be called whenever the source DependencyProperty changes. 
/// </summary> 
public class DependencyPropertyChangedHelper : DependencyObject 
{ 
    /// <summary> 
    /// Constructor for the helper. 
    /// </summary> 
    /// <param name="source">Source object that exposes the DependencyProperty you wish to monitor.</param> 
    /// <param name="propertyPath">The name of the property on that object that you want to monitor.</param> 
    public DependencyPropertyChangedHelper(DependencyObject source, string propertyPath) 
    { 
     // Set up a binding that flows changes from the source DependencyProperty through to a DP contained by this helper 
     Binding binding = new Binding 
     { 
      Source = source, 
      Path = new PropertyPath(propertyPath) 
     }; 
     BindingOperations.SetBinding(this, HelperProperty, binding); 
    } 

    /// <summary> 
    /// Dependency property that is used to hook property change events when an internal binding causes its value to change. 
    /// This is only public because the DependencyProperty syntax requires it to be, do not use this property directly in your code. 
    /// </summary> 
    public static DependencyProperty HelperProperty = 
     DependencyProperty.Register("Helper", typeof(object), typeof(DependencyPropertyChangedHelper), new PropertyMetadata(null, OnPropertyChanged)); 

    /// <summary> 
    /// Wrapper property for a helper DependencyProperty used by this class. Only public because the DependencyProperty syntax requires it. 
    /// DO NOT use this property directly. 
    /// </summary> 
    public object Helper 
    { 
     get { return (object)GetValue(HelperProperty); } 
     set { SetValue(HelperProperty, value); } 
    } 

    // When our dependency property gets set by the binding, trigger the property changed event that the user of this helper can subscribe to 
    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var helper = (DependencyPropertyChangedHelper)d; 
     helper.PropertyChanged(d, e); 
    } 

    /// <summary> 
    /// This event will be raised whenever the source object property changes, and carries along the before and after values 
    /// </summary> 
    public event EventHandler<DependencyPropertyChangedEventArgs> PropertyChanged = delegate { }; 
} 
} 

Cách sử dụng XAML:

<TextBlock Grid.Row="0" 
     x:Name="WritingMenuTitle" 
     HorizontalAlignment="Left" 
     FontSize="32" 
     FontWeight="SemiBold" 
     Text="{Binding WritingMenu.Title}" 
     TextAlignment="Left" 
     TextWrapping="Wrap"/> 

xaml.cs Cách sử dụng:

Behaviors.DependencyPropertyChangedHelper helper = new Behaviors.DependencyPropertyChangedHelper(this.WritingMenuTitle, Models.CommonNames.TextBlockText); 
helper.PropertyChanged += viewModel.OnSenarioTextBlockTextChangedEvent; 

viewmodel.cs Cách sử dụng:

public async void OnSenarioTextBlockTextChangedEvent(object sender, DependencyPropertyChangedEventArgs args) 
{ 
StringBuilder sMsg = new StringBuilder(); 

try 
{ 
    Debug.WriteLine(String.Format(".....WritingMenuTitle : New ({0}), Old ({1})", args.NewValue, args.OldValue)); 
} 
catch (Exception msg) 
{ 
    #region Exception 
    ..... 
    #endregion 
} 
} 
16

của nó dễ dàng hơn mà lol. Câu trả lời muộn nhưng đơn giản hơn nhiều.

// assume textBlock is your TextBlock 
DependencyPropertyDescriptor dp = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock)); 

dp.AddValueChanged(textBlock, (object a, EventArgs b) => 
{ 
     MessageBox.Show("text changed"); 
}); 
+1

Bạn đang đặt dp.AddValueChanged ở đâu? – wbt11a

+1

nạp hoặc xây dựng sẽ âm thanh như một nơi tốt. –

Các vấn đề liên quan