実行時にのみ機能します。new InputNodeRed() を MyNode の Node コントロールにバインドすると、設計時ではなくプログラムの実行中にのみ静的リソースが表示されます。私は何を間違っていますか。(詳細については、コンバーター コードのコメントを参照してください)
カスタム コントロール ノード
public class Node : Control
{
static Node()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Node), new FrameworkPropertyMetadata(typeof(Node)));
}
public BaseData.Node MyNode
{
get { return (BaseData.Node)GetValue(MyNodeProperty); }
set { SetValue(MyNodeProperty, value); }
}
public static readonly DependencyProperty MyNodeProperty =
DependencyProperty.Register("MyNode", typeof(BaseData.Node), typeof(Node), new PropertyMetadata(null));
}
}
ノードコンバーター
public class NodeTypeToDataTemplateConverter :IValueConverter
{
public DataTemplate RedNode { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//if(value == null) //binding new InputNodeRed(), at design time value == null
// return null;
if (value is BaseData.InputNodeRed || value is BaseData.OutputNodeRed)
return RedNode;
return null;
}
}
このコードif(value == null) return null;
は、値が null であるかどうかを確認するためだけに配置したもので、設計時に null を返します。
ノード テンプレート
<local:NodeTypeToDataTemplateConverter x:Key="NodeTypeConverter"
RedNode="{StaticResource RedNode}" />
<Style TargetType="{x:Type local:Node}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Node}">
<ContentPresenter ContentTemplate="{Binding MyNode, Converter={StaticResource NodeTypeConverter}, RelativeSource={RelativeSource TemplatedParent}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
データクラス
abstract public class Node { }
abstract public class InputNode : Node { }
abstract public class OutputNode : Node { }
public class OutputNodeRed : OutputNode { }
public class InputNodeRed : InputNode { }
バインドは明らかに機能しますが、設計時には機能しません。