Bind to a value defined in the Settings

First, you need to add a custom XML namespace that will design the namespace where the settings are defined: xmlns:properties=”clr-namespace:TestSettings.Properties” Then, in your XAML file, access the default settings instance using the following syntax: {x:Static properties:Settings.Default} So here is the final result code: <ListBox x:Name=”lb” ItemsSource=”{Binding Source={x:Static properties:Settings.Default}, Path=Names}” /> Source: WPF – How to … Read more

Custom DateTime model binder in Asp.net MVC

you can change the default model binder to use the user culture using IModelBinder public class DateTimeBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value); return value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture); } } public class NullableDateTimeBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); … Read more

How to bind multiple values to a single WPF TextBlock?

You can use a MultiBinding combined with the StringFormat property. Usage would resemble the following: <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=”{}{0} + {1}”> <Binding Path=”Name” /> <Binding Path=”ID” /> </MultiBinding> </TextBlock.Text> </TextBlock> Giving Name a value of Foo and ID a value of 1, your output in the TextBlock would then be Foo + 1. Note: This … Read more

WPF Error: Cannot find governing FrameworkElement for target element

Sadly any DataGridColumn hosted under DataGrid.Columns is not part of Visual tree and therefore not connected to the data context of the datagrid. So bindings do not work with their properties such as Visibility or Header etc (although these properties are valid dependency properties!). Now you may wonder how is that possible? Isn’t their Binding … Read more

Binding ItemsSource of a ComboBoxColumn in WPF DataGrid

Pls, check if DataGridComboBoxColumn xaml below would work for you: <DataGridComboBoxColumn SelectedValueBinding=”{Binding CompanyID}” DisplayMemberPath=”Name” SelectedValuePath=”ID”> <DataGridComboBoxColumn.ElementStyle> <Style TargetType=”{x:Type ComboBox}”> <Setter Property=”ItemsSource” Value=”{Binding Path=DataContext.CompanyItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}” /> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType=”{x:Type ComboBox}”> <Setter Property=”ItemsSource” Value=”{Binding Path=DataContext.CompanyItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}” /> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> Here you can find another solution for the problem you’re facing: … Read more

Why RelayCommand

Commands are used to separate the semantics and the object that invokes a command from the logic that executes the command i.e. it separates UI component from the logic that needs to be executed on command invocation. So, that you can test business logic separately using test cases and also your UI code is loosely … Read more