ボタンに基づいてカスタムコントロールを作成しています。ボタンの幅をクラスのプロパティにバインドします。私はthis、this、およびthisを見ましたが、それらは私が探しているものではなく、動作しません。プロパティをカスタムコントロールの別のプロパティにバインドします。
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControl">
<Style TargetType="{x:Type local:MyCustomControl}" BasedOn = "{StaticResource {x:Type Button}}">
<Setter Property = "Background" Value = "LightSalmon" />
<Setter Property = "Foreground" Value = "Blue"/>
<Setter Property = "Height" Value = "50"/>
<Setter Property = "Width" Value = "{Binding MyCustomControl.TextBinding}"/>
<Setter Property = "VerticalAlignment" Value = "Top"/>
<Setter Property = "Margin" Value="10"/>
</Style>
</ResourceDictionary>
MyCustomControl.cs
namespace CustomControl
{
public class MyCustomControl : Button
{
double m_textBinding = 50;
public double TextBinding
{
get { return m_textBinding; }
set { m_textBinding = value; }
}
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl),
new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
}
}
必要であれば、私は "セッター" 機能を使用することができ、かつ、手動"Width = value;"
を指定するが、私は使用することを好みます拘束力。現在、"{Binding MyCustomControl.TextBinding}"
は機能していません。
を 'TextBinding'は、コントロールの幅を定義するプロパティの奇妙な名前のようです。 – Clemens
雅、私はそれが文字列ではなく二重であることを実感しました。私は怠惰なので名前をつけただけです。これは単なるテストです。 –