1

私は WPF を試しており、CustomControl を使用して独自のパネル タイプを作成したので、すべてのパネルを背景や角の丸いボックスのように見せることができます。

私が抱えていない問題は、実行時に TemplateBinding を使用してバインドされたアイテムのプロパティを変更したいということです。

私の問題は、コードを介してこれを変更しても機能しないことです。

誰かが私のエラーを見つけて、私が少し密集している場所を教えてくれることを望んでいました.

これは contentControl を見つけて値を変更します。バインディングがアクティブでないかのように、これはアプリケーションに表示されません。

誰かが助けてくれることを願っています。

コントロール テンプレート。

<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}" HorizontalAlignment="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>

コンテンツ コントロール

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"));
}

コンテンツ コントロールを使用するための xaml

そして最後に、プロパティを変更するコード

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

1 に答える 1

2

間違いを見つけて、思った通りバカになりました!!

ContentControlで、依存関係プロパティを適切に設定していませんでした。

contentcontrolのプロパティを次のように変更すると、問題が修正されました

public string Title
{
    get { return (string)this.GetValue(TitleProperty); }
    set { this.SetValue(TitleProperty, value); } 
}
于 2011-08-12T08:58:39.233 に答える