2014-05-02 70 views
0

我正在使用一個附屬屬性來從TextBlock對TargetUpdated事件進行subscrive,所以我可以在每次文本更改時收到通知。WPF TargetUpdated事件沒有觸發ContentControl

使用下面的XAML:

<DataTemplate DataType="{x:Type targetUpdatedApp:Item}"> 
     <targetUpdatedApp:LabelControl Text="{Binding Text, NotifyOnTargetUpdated=True}" Style="{StaticResource LabelTemplateStyle}"/> 
    </DataTemplate> 
</Window.Resources> 

<StackPanel> 
    <ListBox ItemsSource="{Binding Items}"> 
     <!--<ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Text, NotifyOnTargetUpdated=True}" targetUpdatedApp:DesiredWidth.DesiredMinWidth="120"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate>--> 
    </ListBox> 

    <Button Click="ButtonBase_OnClick">Button</Button> 
</StackPanel> 

這裏是我的AttachedProperty代碼:

public class DesiredWidth 
{ 
    public static readonly DependencyProperty DesiredMinWidthProperty = 
     DependencyProperty.RegisterAttached(
      "DesiredMinWidth", typeof (double), 
      typeof (TextBlock), new PropertyMetadata(OnDesiredMinWidthChanged)); 

    public static double GetDesiredMinWidth(DependencyObject obj) 
    { 
     return (double) obj.GetValue(DesiredMinWidthProperty); 
    } 

    public static void SetDesiredMinWidth(DependencyObject obj, double value) 
    { 
     obj.SetValue(DesiredMinWidthProperty, value); 
    } 

    static void OnDesiredMinWidthChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
    { 
     var textBlock = obj as TextBlock; 
     if (textBlock == null) 
     { 
      return; 
     } 

     if (args.NewValue != null) 
     { 
      textBlock.TargetUpdated += OnTextBoxTargetUpdated;  
     } 
    } 

    static void OnTextBoxTargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e) 
    { 
     if (e.Property == TextBlock.TextProperty) 
     { 

     } 
    } 
} 

綁定到ListBox的Items集合是ObservableCollection,其上的項目實現了INotifyPropertyChanged。 如果我取消註釋ListBox.ItemTemplate的代碼並使用它代替風格,它的工作正常,但我使用的LabelControl(基本上有一個文本DependencyProperty)描述的風格TargetUpdated事件訂閱AttachedProperty永遠不會被解僱。

有人可以給我一些關於這個問題的幫助嗎? 在此先感謝。

PS:從留言補充:

<Style x:Key="LabelTemplateStyle" TargetType="{x:Type argetUpdatedApp:LabelControl}">  
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type targetUpdatedApp:LabelControl}"> 
       <TextBlock Text="{TemplateBinding Text}" TargetUpdatedApp:DesiredWidth.DesiredMinWidth="120"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

添加缺少的風格: <形式X:鍵= 「LabelTemplateStyle」 的TargetType = 「{x:類型targetUpdatedApp:LabelControl}」>

+0

如果你對問題的評論不是那麼好。更好閱讀 –

回答

0

NotifyOnTargetUpdated將只適用於結合它的設置上,而不是包含在元素綁定。您在LabelControl上設置了此設置,但事件處理程序連接到其ControlTemplate中的TextBlock,該通知不在通知中。用途:

<ControlTemplate TargetType="{x:Type targetUpdatedApp:LabelControl}"> 
    <TextBlock Text="{TemplateBinding Text, NotifyOnTargetUpdated=True}" targetUpdatedApp:DesiredWidth.DesiredMinWidth="120"/> 
</ControlTemplate> 

另一種方法可以做到這一點,這將不要求您修改綁定,是使用,而不是元素本身的Text財產另一個附加屬性:

<TextBlock DesiredWidth.Text="{Binding Text}" DesiredWidth.MinWidth="120" /> 

而且在DesiredWidth您可以添加一個OnTextChanged回調函數,將值傳遞給TextBlock的Text,並執行任何其他處理。

+0

感謝您的反饋。 我試過第一種方法,代碼無法編譯,因爲我使用TemplateBinding NotifyOnTargetUpdated不受支持。 第二種方法在啓動時引發異常,我需要創建的附加屬性類型是BindingExpression而不是字符串。 –

+0

@HelderCosta附加的屬性應該可以工作。確保你正確地聲明它。我在你的問題中注意到你在'RegistereAttached'中使用'typeof(TextBlock)'。這應該是它聲明的類型('typeof(DesiredWidth)')。 – nmclean