开发者

Silverlight 4, pass style to control inside user control

I created a UserControl which contains an AutoCompleteBox to simplify the bindings. Now requirements have changed and I need to pass a style to the AutoCompleteBox. I added a DependencyProperty for the style to my UserControl. The binding works but the styling is not applied.

This is my code behind:

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)
    {
      开发者_JS百科  customAutoCompleteBox.ContentStyle = newValue;
    }
}

And the 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>

I hope someone can point out what I'm doing wrong.

Cheers AC


The code you have above adds a dependency property to your custom control called ContentStyle. However, the XAML you have there would attempt to bind the Style of the control to a value on the DataContext called "ContentStyle". These are two different things.

What you want is to be able to assign a new style to the control programmatically. Unfortunately, this is only possible once, as there is no way to "unset" a style once it has been set. This is from the Silverlight forum.

http://forums.silverlight.net/forums/p/11670/37375.aspx

However, it IS possible to set the style once, so the easiest way to solve this problem is as suggested in the post. Programmatically remove your custom control from the control tree, set your new style(s), and add it back to the parent control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜