没有为附加属性调用我的OnPropertyChangedCallback。 我希望在运行应用程序时默认调用此方法。
有什么建议吗?
附属财产:
namespace FileModifier.Behaviors
{
public class AttachedProperties : DependencyObject
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(AttachedProperties), new PropertyMetadata(null, OnPropertyChangedCallback));
public static void SetText(DependencyObject d, string value)
{
d.SetValue(TextProperty, value);
}
public static string GetText(DependencyObject d)
{
return d.GetValue(TextProperty) as string;
}
private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var richEditBox = d as RichTextBox;
var textRange = new TextRange(richEditBox.Document.ContentStart, richEditBox.Document.ContentEnd);
throw new NotImplementedException();
}
}
}
XAML:
<RichTextBox x:Name="FileContentControl" IsReadOnly="True"
behaviors:AttachedProperties.Text="{Binding ElementName=FileListView, Path=SelectedItem,
Converter={StaticResource FilePathToContentConverter}}"
VerticalAlignment="Center" Padding="10" />
。 。
<ListView x:Name="FileListView" ItemsSource="{Binding FilePaths}" Grid.Column="2" Grid.Row="0"
SelectedItem="{Binding SelectedFilePath}"
VerticalAlignment="Center"
Margin="5">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource TextTruncationConverter}}" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="Padding" Value="5" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
答案 0 :(得分:0)
您没有注册附加财产,而是正常DependencyProperty
。您应该使用DependencyProperty.RegisterAttached
代替。此外,如果您的类仅包含附加属性,它也可以是静态类。它不必从DependencyObject
public static class AttachedProperties
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.RegisterAttached("Text", typeof(string), typeof(AttachedProperties), new PropertyMetadata(null, OnPropertyChangedCallback));
public static void SetText(DependencyObject d, string value)
{
d.SetValue(TextProperty, value);
}
public static string GetText(DependencyObject d)
{
return d.GetValue(TextProperty) as string;
}
private static void OnPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var richEditBox = d as RichTextBox;
var textRange = new TextRange(richEditBox.Document.ContentStart, richEditBox.Document.ContentEnd);
throw new NotImplementedException();
}
}