开发者

WPF CustomControl ControlTemplate update TemplateBinding at Runtime

I have been experimenting with WPF and using a CustomControl have made my own panel type so I can make all my panels look like a box with backgrounds, rounded corners etc.

The trouble I am not having is I want to change the property of an item at runtime that is bound using TemplateBinding.

My issue is changing this through code is not working.

Was hoping someone could spot my error and tell me where I am being slightly dense.

This finds the contentControl and changes the value, this just never appears in the application, as if the binding is not active.

Hope someone can help.

The control Template.

<ControlTemplate x:Key="ContentBoxControlTemplate" TargetType="{x:Type local:ContentBox}"><Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.124*"/>
                <RowDefinition Height="0.876*"/>
            </Grid.RowDefinitions>
            <Border BorderBrush="Black" BorderThickness="0" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" CornerRadius="20" Grid.RowSpan="2" Style="{TemplateBinding Style}" />
            <Rectangle HorizontalAlignment="Stretch" Height="Auto" RadiusY="0" StrokeThickness="0" VerticalAlignment="Stretch" Width="Auto" Margin="10,0,10,20" Fill="White" Grid.Row="1"/>
            <Rectangle Fill="{DynamicResource TopInnerShadow}" Horizonta开发者_运维百科lAlignment="Stretch" Height="Auto" RadiusY="0" StrokeThickness="0" VerticalAlignment="Stretch" Width="Auto" Margin="10,0,10,20" Grid.Row="1"/>
            <Rectangle Fill="{DynamicResource RightInnerShadow}" HorizontalAlignment="Stretch" Height="Auto" RadiusY="0" StrokeThickness="0" VerticalAlignment="Stretch" Width="Auto" Margin="10,0,10,20" Grid.Row="1"/>

            <TextBlock Grid.Row="0" x:Name="txtBoxHeading" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" VerticalAlignment="Stretch" d:LayoutOverrides="Width, Height" Foreground="White" FontSize="36" FontFamily="Arial" Margin="20,5"/>

            <Grid Grid.Row="1">
                <ContentControl Content="{TemplateBinding Content}" Margin="10,0,10,20"/>
            </Grid>

        </Grid>
    </ControlTemplate>

The ContentControl

public class ContentBox : ContentControl

{
    static ContentBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ContentBox), new FrameworkPropertyMetadata(typeof(ContentBox)));
    }

    private string _title = "";
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    // Dependency Property
    public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ContentBox), new FrameworkPropertyMetadata("Title"));
}

The xaml for using the content control

And finally the code to change the property

(this.Parent as ContentBox).Title = "Campaign: " + campaigns[0].Name;


I found my mistake, and I was as I thought, being silly!!

In the ContentControl I was not setting the dependancy property properly.

Changing the property in the contentcontrol to the following fixed my problem

public string Title
{
    get { return (string)this.GetValue(TitleProperty); }
    set { this.SetValue(TitleProperty, value); } 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜