Default value for comboBox
Hi I try set default value for comboBox.
XAML:
<ComboBox Name="StatusCombo"
Style="{StaticResource statusComboStyle}"
SelectedValuePath="StatusMsg"
SelectedValue="{Binding Path=SelectedStatus}"/>
I use caliburn.Micro. I bind List<string, StatusItem>
to ComboBox, it works good.
Status item is simple class, here is it:
public class StatusItem
{
public string StatusMsg { get; set; }
public BitmapImage StatusImg { get; set; }
}
App.xaml
I define empty string in app.xaml
<System:String x:Key="empty"></System:String>
statusComboStyle is here:
<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>
But something must be wrong, because I get this compile error:
{"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
I check in data trigger if SelectedStatus is empty string, if value is empty string I set frist item in comboBox.
You do not need to create your own empty string, string has a static field for that, so you could set it in the style like this:
Value="{x:Static System:String.Empty}"
And why don't you just use a normal setter in your style right away?
<Style x:Key="statusComboStyle" TargetType="{x:Type ComboBox}">
<Setter Property="SelectedIndex" Value="0"/>
</Style>
(You should make sure that there is at least one item in the ComboBox)
Binding="{Binding Path=Name.Length, FallbackValue=0, TargetNullValue=0}" Value="0">
精彩评论