こんにちは、私はcomboBoxのデフォルト値を設定しようとしています。comboBoxのデフォルト値
XAML:
<ComboBox Name="StatusCombo"
Style="{StaticResource statusComboStyle}"
SelectedValuePath="StatusMsg"
SelectedValue="{Binding Path=SelectedStatus}"/>
私はcaliburn.Microを使用しています。私はComboBoxにList<string, StatusItem>
をバインドして、うまくいきます。ここ
public class StatusItem
{
public string StatusMsg { get; set; }
public BitmapImage StatusImg { get; set; }
}
App.xaml
私がapp.xaml
<System:String x:Key="empty"></System:String>
statusComboStyleに空の文字列を定義している:
ステータス項目は、それはここで、単純なクラスです
<Style x:Key="statusComboStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path= SelectedStatus}" Value="{StaticResource empty}">
<Setter Property="SelectedIndex" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
しかし、私は、このコンパイルエラー得るために何かが、間違っている必要があります。
{"No matching constructor found on type 'System.String'. You can use the Arguments or FactoryMethod directives to construct this type."}
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Spirit.Views.LogOnView.InitializeComponent() in c:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_Caliburn_Micro_v1.0\Views\LogOnView.xaml:line 1
at Spirit.Views.LogOnView..ctor() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_Caliburn_Micro_v1.0\Views\LogOnView.xaml.cs:line 24
をSelectedStatusが空の文字列である場合、私は値が空の文字列である場合、私はコンボボックスに延期項目を設定し、データトリガでチェック。
Value="{x:Static System:String.Empty}"
そして、なぜあなただけを使用しないでください:あなたはこのようなスタイルでそれを設定することができるように
あなたは明示的に 'SelectedIndex'プロパティを設定しない場合はどうなりますか?バインドするプロパティのデフォルト値で十分です。 – ChrisF
ComboBoxのItemsSourceにバインドしているため、SelectedIndexのSetterは機能しません。代わりに、ChrisFが指摘するように、最初にSelectedStatusをビューモデルに設定する必要があります。 – HappyNomad