私は同じ方法でコンテナになることを意図したWPFコントロールを作成していますBorder
とScrollViewer
はコンテナです。これはEllipsisButtonControl
と呼ばれ、コンテンツの右側に省略記号ボタンを配置することになっています。ここでは、それが使用されるために、私はつもりかの例です:ここでコンテナとして機能するWPFコントロールでは、どのようにコンテンツを配置しますか?
<local:EllipsisButtonControl>
<TextBlock Text="Testing" />
</local:EllipsisButtonControl>
はEllipsisButtonControl
のためのXAMLです:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class EllipsisButtonControl
{
public EllipsisButtonControl()
{
InitializeComponent();
}
public static string GetCommand(DependencyObject obj)
{
return (string)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, string value)
{
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
name: "Command",
propertyType: typeof(ICommand),
ownerType: typeof(EllipsisButtonControl),
defaultMetadata: new UIPropertyMetadata());
}
}
このdoesnの:
<ContentControl
x:Class="WpfApplication1.EllipsisButtonControl"
x:Name="ContentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" Content="{Binding ElementName=ContentControl, Path=Content}" />
<Button Grid.Column="1" Command="{Binding ElementName=ContentControl, Path=Command}" Margin="3,0" Width="30" Height="24" MaxHeight="24" VerticalAlignment="Stretch" Content="..." />
</Grid>
</ContentControl>
そしてここでは、背後にあるコードがあります仕事はありません。デザイナーはSystem.Runtime.Remoting.RemotingException
でクラッシュします。
XAML EllipsisButtonControl
XAMLのContentPresenter
へのバインディングは間違っていると思いますが、それを正しく行う方法はわかりません。その行がコントロールの内容を参照するための適切な構文は何ですか? (例えば使用例で定義されてTextBlock
)
編集:
突くが、(作業コードを含む)以下の総合的な答えを提供しますが、私の最初の誤解を共有するかもしれない他の人の利益のために、私がまとめてみましょうここでの重要な概念:コンテナコントロールはそれ自体がコンテンツを配置することはできません。呼び出し元のXAMLがコンテンツを表示する方法を変更するテンプレートを定義することによって、望ましい効果が得られます。このソリューションの残りの部分は、その前提に従います。生成されたレンダリングしなければならないContentPresenter
を含んで既存のコードあなたはこのContentPresenter
表示EllipsesButtonControl
の生成されたコンテンツを作っている、ではこの
<ContentPresenter Grid.Column="0" Content={Binding Content} />
で
<ContentPresenter Grid.Column="0" Content="{Binding ElementName=ContentControl, Path=Content}" />
:
Commandは通常の依存関係プロパティではなく、接続されたプロパティですか。 – Clemens
ええと...私がコピーした例がそうしていたので...あなたの質問でプロンプトが表示され、添付プロパティを調べて、DependencyProperty.RegisterAttached()、DependencyProperty.RegisterAttached()をLine 23で使用する必要があることを知りました。ありがとう。 – Lork
* "コンテナコントロールはコンテンツ自体を置くことはできません" * - それは可能ですが、コンテンツは1つしかないので、コンテンツを外側に設定すると内側に置き換えられます。あなたが望むのは、コンテンツが内部に置かれる(フレーミング)ことです(=テンプレート)。 – poke