Я создал UserControl, который содержит AutoCompleteBox для упрощения привязок. Теперь требования изменились, и мне нужно передать стиль AutoCompleteBox. Я добавил DependencyProperty для стиля в свой UserControl. Привязка работает, но стиль не применяется.
Это мой код:
public partial class CustomAutoCompleteBox
{
public static readonly DependencyProperty ContentStyleProperty = DependencyProperty.Register(
"ContentStyle",
typeof(Style),
typeof(CustomAutoCompleteBox),
new PropertyMetadata(OnContentStyleChanged));
/// <summary>
/// Initializes a new instance of the <see cref="CustomAutoCompleteBox"/> class.
/// </summary>
public CustomAutoCompleteBox()
{
this.InitializeComponent();
}
/// <summary>
/// Gets or sets ContentStyle.
/// </summary>
public Style ContentStyle
{
get
{
return (Style)this.GetValue(ContentStyleProperty);
}
set
{
this.SetValue(ContentStyleProperty, value);
}
}
private static void OnContentStyleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var customAutoCompleteBox = obj as CustomAutoCompleteBox;
var newValue = e.NewValue as Style;
if (customAutoCompleteBox != null && newValue != null)
{
customAutoCompleteBox.ContentStyle = newValue;
}
}
И XAML:
<Grid x:Name="LayoutRoot">
<Input:AutoCompleteBox Style="{Binding ContentStyle}"
MinimumPrefixLength="0"
ItemTemplate="{StaticResource DescriptionItemTemplate}"
ValueMemberBinding="{Binding Description, Mode=TwoWay}"
SelectedItem="{Binding Value, ValidatesOnDataErrors=True, Mode=TwoWay}"
ItemsSource="{Binding Values}"
Text="{Binding Text, Mode=TwoWay}"
Behaviors:AutoCompleteBoxBehaviors.PopulatingCommand="{Binding PopulationCommand}"
Behaviors:AutoCompleteBoxBehaviors.ItemFilterPredicate="{Binding ItemFilterPredicate}"/>
</Grid>
Я надеюсь, что кто-то может указать, что я делаю неправильно.
Приветствие
AC