パラメータなしのコンストラクタなしでユーザーコントロールがあります。それを呼びましょうWithoutDefaultConstructor
。別のコントロール(と呼ばれる)のXAMLコードにWithoutDefaultConstructor
呼び出されたものを挿入したいと思います。ただし、次のコンパイラエラーが発生します。myControl
MainWindow
タイプ'WithoutDefaultConstructor'はName属性を持つことができません。値の型とデフォルトのコンストラクターのない型は、ResourceDictionary内の項目として使用できます。
パラメータなしのコンストラクタをに追加せずにこれを修正するにはどうすればよいWithoutDefaultConstructor
ですか?
内容は次のMainWindow.xaml
とおり です。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow"
Height="350" Width="525">
<WpfApplication1:WithoutDefaultConstructor Name="myControl">
</WpfApplication1:WithoutDefaultConstructor>
</Window>
内容は次のWithoutDefaultConstructor.xaml.cs
とおりです。
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class WithoutDefaultConstructor : UserControl
{
private int I_really_need_to_initialize_this_int;
public WithoutDefaultConstructor(int i)
{
InitializeComponent();
I_really_need_to_initialize_this_int = i;
}
}
}