wpfおよび命名コーンラムのコードビハインド静的/動的プロパティ
-
29-10-2019 - |
質問
ボタンスタイルとベクトルグラフィックを備えたボタンがXAMLに定義されているボタンがあります。
<Button
Height="48"
Width="48"
mvp:Presenter.Action="CreateStudentApplication"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Row="5"
Grid.Column="3"
Style="{DynamicResource BaseButton2}">
<ContentControl Template="{StaticResource ApplicationVector}"/>
</Button>
Code-Behindには、これに似た新しいボタンをスタックパネルに動的に追加する方法があります。簡単に言えば、それは次のように何かをします
var button = new Button();
button.Height = 48;
button.Width = 48;
button.Tag = x.ID;
button.SetResourceReference(TemplateProperty, "ApplicationVector");
button.SetResourceReference(StyleProperty, "BaseButton2");
これが奇妙な部分です - それはベクトルグラフィックのみを表示し、その背後にあるボタンはありません。最後から2番目のライン(ベクトル参照のある線)を削除すると、スタイリングされたボタンが表示されます!テンプレートの設定はスタイルをオーバーライドしていると仮定していますが、XAMLで友好的に再生するようです。また、TemplatePropertyのContentProperty Instideadを設定しようとしましたが、これによりタイプの文字列が生まれました。何か案は?ありがとうございました!
解決
XAMLとコードの背後には同等ではありません。 XAMLでは、設定しています Content
ボタンのプロパティ ContentControl
. 。背後のコードでは、設定しています Template
(また Content
)財産になる ApplicationVector
資源。
設定する必要があります Content
ボタンのプロパティは ContentControl
だれの Template
プロパティはあなたになるように設定されています ApplicationVector
資源。
var contentControl = new ContentControl();
contentControl.SetResourceReference(TemplateProperty, "ApplicationVector");
button.Content = contentControl;
所属していません StackOverflow