Multiple Language Binding ItemsSource - c#

I want to change my single language project into multiple language, so I use ResourceDictionary to do it like this:
XAML
<Button Content="{DynamicResource LanguageSetting}" Click="btn_LanguageSetting_Click"/>
Code Behind
public static string windowCurrentLanguageFile = "Language/en.xaml";
private void btn_LanguageSetting_Click(object sender, RoutedEventArgs e)
{
windowCurrentLanguageFile = windowCurrentLanguageFile == "Language/en.xaml"
? "Language/ch.xaml"
: "Language/en.xaml";
var rd = new ResourceDictionary() { Source = new Uri(windowCurrentLanguageFile, UriKind.RelativeOrAbsolute) };
if (this.Resources.MergedDictionaries.Count == 0)
this.Resources.MergedDictionaries.Add(rd);
else
this.Resources.MergedDictionaries[0] = rd;
}
This works fine for me. But I have an ItemsControl
<ItemsControl ItemsSource="{Binding ItemOperate}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewmodel:SelectableViewModel}">
<Border x:Name="Border" Padding="0,8,0,8" BorderThickness="0 0 0 1" BorderBrush="{DynamicResource MaterialDesignDivider}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Checkerz" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ToggleButton VerticalAlignment="Center" IsChecked="{Binding IsSelected}"
Style="{StaticResource MaterialDesignActionLightToggleButton}"
Content="{Binding Code}" />
<StackPanel Margin="8 0 0 0" Grid.Column="7">
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock Text="{Binding Description}" />
</StackPanel>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource MaterialDesignSelection}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Which Binding to the ViewModel like this:
public class SelectableViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected == value) return;
_isSelected = value;
OnPropertyChanged();
}
}
private char _code;
public char Code
{
get { return _code; }
set
{
if (_code == value) return;
_code = value;
OnPropertyChanged();
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged();
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
if (_description == value) return;
_description = value;
OnPropertyChanged();
}
}
}
And
public MainViewModel()
{
_itemOperate = CreateData();
}
private static ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>
{
new SelectableViewModel
{
Code = 'E',
Name = "Erase",
Description = "Erase The MCU Chip By Page"
},
new SelectableViewModel
{
Code = 'D',
Name = "Detect",
Description = "Detect The MCU Flash",
},
new SelectableViewModel
{
Code = 'P',
Name = "Programming",
Description = "Programming The MCU Chip By Hex File",
},
new SelectableViewModel
{
Code = 'V',
Name = "Verify",
Description = "Verify The Downing Code",
},
new SelectableViewModel
{
Code ='L',
Name = "Lock",
Description = "Lock The Code To Protect The MCU",
}
};
}
So how should I change this into multiple language?

First of all, I would recommend you to change your localization engine.
There are a lot of different ways.
There is the simplest variant:
https://www.codeproject.com/Articles/299436/WPF-Localization-for-Dummies
Also, this tool will help you to manage your resources files:
https://marketplace.visualstudio.com/items?itemName=TomEnglert.ResXManager
And the answer to your question:
If you want to localize your model, you should create it using resources dictionary but not hard coded strings.
It's quite easy if you implement your localization engine like in mentioned
article.
{
return new ObservableCollection<SelectableViewModel>
{
new SelectableViewModel
{
Code = 'E',
Name = YourResourcesProject.Resources.Erase,
Description = YourResourcesProject.Resources.EraseTheMCUChipByPage
},
new SelectableViewModel
{
Code = 'D',
Name = YourResourcesProject.Resources.Detect,
Description = YourResourcesProject.Resources.DetectTheMCUFlash
},
new SelectableViewModel
{
Code = 'P',
Name = YourResourcesProject.Resources.Programming,
Description = YourResourcesProject.Resources.ProgrammingTheMCUChipByHexFile
},
new SelectableViewModel
{
Code = 'V',
Name = YourResourcesProject.Resources.Verify,
Description = YourResourcesProject.Resources.VerifyTheDowningCode
},
new SelectableViewModel
{
Code ='L',
Name = YourResourcesProject.Resources.Lock,
Description = YourResourcesProject.Resources.LockTheCodeToProtectTheMCU
}
};
}

Don't change anything...make your collection of SelectableViewModel into a XML and change the CreateData to load it into the localized version - you can either have a file for each language or mix all together

20 lines of code....as you want - cannot more explicit than that :-(
public class Selectable(View)Model
{
[XmlAttribute]
public string Code { get; set; }
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public string Description { get; set; }
}
///you can deserialize your view model directly
private ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>( Deserialize( file_name_code_lang.xml, SelectableViewModel) );
}
// or going through a model class
private ObservableCollection<SelectableViewModel> CreateData()
{
return new ObservableCollection<SelectableViewModel>( Deserialize( file_name_code_lang.xml, SelectableModel ).Foreach(p=> new SelectableViewModel(p) );
}
static public object Deserialize(string filePath, Type objType)
{
object objToDeserialize = null;
XmlTextReader xmlReader = null;
XmlSerializer xmls = null;
try
{
xmlReader = new XmlTextReader(filePath);
xmls = new XmlSerializer(objType);
objToDeserialize = xmls.Deserialize(xmlReader);
}
catch (Exception err)
{
BusinessLogger.Manage(err);
return null;
}
finally
{
xmls = null;
xmlReader.Close();
}
return objToDeserialize;
}

Related

How to update/convert mumeric TextBox value when changing value's unit using a ComboBox? Value normalization based on current unit?

I would like to have a converter system for my Xamarin and WPF project. I don't want to save any units in the database, so I want directly convert the textbox-values when user change the unit.
I made public a few Observable Collections like;
public class AreaList : ObservableCollection<Unit>
{
public AreaList() : base()
{
Add(new Unit("mm²"));
Add(new Unit("cm²"));
Add(new Unit("dm²"));
Add(new Unit("m²"));
}
}
public class Unit
{
private string name;
public Unit(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
set { name = value; }
}
}
In the View i bind the collection to my combo box. I gave my TextBox the name of his binding property(Text="{Binding TxtBoxValue}" => x:Name="TxtBoxValue"). The ConvertUnitValueCommand set this name as a string in the view model to know which variable the converter function should use when the unit is changed.
View
<UserControl.Resources>
<c:AreaList x:Key="AreaListData" />
</UserControl.Resources>
<TextBox x:Name="TxtBoxValue"
Text="{Binding Mode=TwoWay, Path=TxtBoxValue, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<ComboBox IsSynchronizedWithCurrentItem="True"
IsEditable="False"
DisplayMemberPath="Name"
SelectedItem="{Binding Unit,Mode=OneWayToSource}"
ItemsSource="{Binding Source={StaticResource AreaListData}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding ConvertUnitValueCommand}"
CommandParameter="{Binding ElementName=TxtBoxValue, Path=Name}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
ViewModel
private string ConvertControlName;
private void ConvertUnitValue(object obj)
{
ConvertControlName = obj.ToString();
}
public Unit Unit
{
get => Get<Unit>();
set
{
if (ConvertControlName != null)
{
FieldInfo variable = this.GetType().GetField(ConvertControlName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
//Get the Value from setted Binding Variable
double oldValue = (double)variable.GetValue(this);
//Convert the value
if (oldValue > 0)
{
double newValue = Converts.ConvertUnitValue(Unit, value, oldValue);
variable.SetValue(this, newValue);
}
Set(value);
}
}
Maybe anyone can give me some inspiration to do it better.
The following example normalizes the user input to the base unit m²:
Unit.cs
public class Unit
{
public Unit(string name, decimal baseFactor)
{
this.Name = name;
this.BaseFactor = baseFactor;
}
#region Overrides of Object
/// <inheritdoc />
public override string ToString() => this.Name;
#endregion
public string Name { get; set; }
public decimal BaseFactor { get; set; }
}
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.Units = new List<Unit>()
{
new Unit("mm²", (decimal) (1 / Math.Pow(1000, 2))),
new Unit("cm²", (decimal) (1 / Math.Pow(100, 2))),
new Unit("dm²", (decimal) (1 / Math.Pow(10, 2))),
new Unit("m²", 1)
};
}
private void NormalizeValue()
{
this.NormalizedValue = this.UnitValue * this.SelectedUnit.BaseFactor;
}
private List<Unit> units;
public List<Unit> Units
{
get => this.units;
set
{
this.units = value;
OnPropertyChanged();
}
}
private Unit selectedUnit;
public Unit SelectedUnit
{
get => this.selectedUnit;
set
{
this.selectedUnit = value;
OnPropertyChanged();
NormalizeValue();
}
}
private decimal unitValue;
public decimal UnitValue
{
get => this.unitValue;
set
{
this.unitValue = value;
OnPropertyChanged();
NormalizeValue();
}
}
private decimal normalizedValue;
public decimal NormalizedValue
{
get => this.normalizedValue;
set
{
this.normalizedValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
ManiWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DatContext>
<StackPanel>
<!-- Input -->
<TextBox Text="{Binding UnitValue}" />
<ComboBox ItemsSource="{Binding Units}"
SelectedItem="{Binding SelectedUnit}" />
<TextBlock Text="{Binding NormalizedValue}" />
</StackPanel>
</Window>
Reusable solution
A reusable solution would be to create a custom control, which derives from TextBox and encapsulates the normalization logic and the control design.
The following custom control NormalizingNumericTextBox extends TextBox and converts two way from non-normalized value to normalized and back.
It is basically a TextBox aligned with a ComboBox as Unit selector.
It may not be perfect, but it is ready to use and it just took me about 10 minutes to merge the previous answer into this custom control.
NormalizingNumericTextBox supports any type of unit describing a numeric value.
Just bind the NormalizingNumericTextBox.Units property to collection of any kind of Unit implementation e.g. weight, length, currency, etc.
Bind to NormalizingNumericTextBox.NormalizedValue to get/set the normalized value. Setting this property will convert the value to the current NormalizingNumericTextBox.SelectedUnit.
Bind to NormalizingNumericTextBox.Text for the raw input value.
Ensure that the default Style (see below) is added to the ResourceDictionary inside /Themes/Generic.xaml. Customize this Style to customize appearance.
ManiWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DatContext>
<StackPanel>
<!-- Input -->
<NormalizingUnitTextBox NormalizedValue="{Binding NormalizedValue}"
Units="{Binding Units}"
Width="180" />
<!--
Test to show/manipulate current normalized value of the view model.
An entered normalized value will be converted back to the current NormalizingNumericTextBox.Unit -->
<TextBox Background="Red" Text="{Binding NormalizedUnitValue}"/>
</StackPanel>
</Window>
Unit.cs
public class Unit
{
public Unit(string name, decimal baseFactor)
{
this.Name = name;
this.BaseFactor = baseFactor;
}
#region Overrides of Object
/// <inheritdoc />
public override string ToString() => this.Name;
#endregion
public string Name { get; set; }
public decimal BaseFactor { get; set; }
}
ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.Units = new List<Unit>()
{
new Unit("m²", 1),
new Unit("dm²", (decimal) (1/Math.Pow(10, 2))),
new Unit("cm²", (decimal) (1/Math.Pow(100, 2))),
new Unit("mm²", (decimal) (1/Math.Pow(1000, 2)))
};
}
public List<Unit> Units { get; set; }
private decimal normalizedValue;
public decimal NormalizedValue
{
get => this.normalizedValue;
set
{
this.normalizedValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
NormalizingNumericTextBox.cs
[TemplatePart(Name = "PART_UnitsItemsHost", Type = typeof(ItemsControl))]
public class NormalizingNumericTextBox : TextBox
{
public static readonly DependencyProperty UnitsProperty = DependencyProperty.Register(
"Units",
typeof(IEnumerable<Unit>),
typeof(NormalizingNumericTextBox),
new PropertyMetadata(default(IEnumerable<Unit>), NormalizingNumericTextBox.OnUnitsChanged));
public IEnumerable<Unit> Units
{
get => (IEnumerable<Unit>) GetValue(NormalizingNumericTextBox.UnitsProperty);
set => SetValue(NormalizingNumericTextBox.UnitsProperty, value);
}
public static readonly DependencyProperty SelectedUnitProperty = DependencyProperty.Register(
"SelectedUnit",
typeof(Unit),
typeof(NormalizingNumericTextBox),
new FrameworkPropertyMetadata(
default(Unit),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
NormalizingNumericTextBox.OnSelectedUnitChanged));
public Unit SelectedUnit
{
get => (Unit) GetValue(NormalizingNumericTextBox.SelectedUnitProperty);
set => SetValue(NormalizingNumericTextBox.SelectedUnitProperty, value);
}
public static readonly DependencyProperty NormalizedValueProperty = DependencyProperty.Register(
"NormalizedValue",
typeof(decimal),
typeof(NormalizingNumericTextBox),
new FrameworkPropertyMetadata(
default(decimal),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
NormalizingNumericTextBox.OnNormalizedValueChanged));
public decimal NormalizedValue
{
get => (decimal) GetValue(NormalizingNumericTextBox.NormalizedValueProperty);
set => SetValue(NormalizingNumericTextBox.NormalizedValueProperty, value);
}
private ItemsControl PART_UnitsItemsHost { get; set; }
private bool IsNormalizing { get; set; }
static NormalizingNumericTextBox()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(NormalizingNumericTextBox),
new FrameworkPropertyMetadata(typeof(NormalizingNumericTextBox)));
}
public NormalizingNumericTextBox()
{
}
private static void OnNormalizedValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var _this = d as NormalizingNumericTextBox;
_this.ConvertNormalizedValueToNumericText();
}
private static void OnSelectedUnitChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as NormalizingNumericTextBox).NormalizeNumericText();
}
private static void OnUnitsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var _this = d as NormalizingNumericTextBox;
_this.SelectedUnit = _this.Units.FirstOrDefault();
}
/// <inheritdoc />
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.PART_UnitsItemsHost = GetTemplateChild("PART_UnitsItemsHost") as ItemsControl;
if (this.PART_UnitsItemsHost == null)
{
throw new InvalidOperationException($"{nameof(this.PART_UnitsItemsHost)} not found in ControlTemplate");
}
this.PART_UnitsItemsHost.SetBinding(
Selector.SelectedItemProperty,
new Binding(nameof(this.SelectedUnit)) {Source = this});
this.PART_UnitsItemsHost.SetBinding(
ItemsControl.ItemsSourceProperty,
new Binding(nameof(this.Units)) {Source = this});
this.SelectedUnit = this.Units.FirstOrDefault();
}
#region Overrides of TextBoxBase
/// <inheritdoc />
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if (this.IsNormalizing)
{
return;
}
NormalizeNumericText();
}
/// <inheritdoc />
protected override void OnTextInput(TextCompositionEventArgs e)
{
// Suppress non numeric characters
if (!decimal.TryParse(e.Text, NumberStyles.Number, CultureInfo.CurrentCulture, out decimal _))
{
e.Handled = true;
return;
}
base.OnTextInput(e);
}
#endregion Overrides of TextBoxBase
private void NormalizeNumericText()
{
this.IsNormalizing = true;
if (decimal.TryParse(this.Text, NumberStyles.Number, CultureInfo.CurrentCulture, out decimal numericValue))
{
this.NormalizedValue = numericValue * this.SelectedUnit.BaseFactor;
}
this.IsNormalizing = false;
}
private void ConvertNormalizedValueToNumericText()
{
this.IsNormalizing = true;
decimal value = this.NormalizedValue / this.SelectedUnit.BaseFactor;
this.Text = value.ToString(CultureInfo.CurrentCulture);
this.IsNormalizing = false;
}
}
Generic.xaml
<ResourceDictionary>
<Style TargetType="NormalizingNumericTextBox">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NormalizingNumericTextBox">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" Margin="0" />
<ComboBox x:Name="PART_UnitsItemsHost" Grid.Column="1" BorderThickness="0" HorizontalAlignment="Right" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
I have not much idea about your code impact but I would suggest you try below design which uses MVVM Pattern which removes tight coupling between UI and Backend.
I have separate out the things here
your XAML will have code like
<TextBox x:Name="unitTextbox"
Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<ComboBox IsSynchronizedWithCurrentItem="True"
IsEditable="False"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedUnit}"
ItemsSource="{Binding AvailableUnits}">
</ComboBox>
Your ViewModel will be like
public class MainVm : Observable
{
#region Private Fields
private double _value;
private ObservableCollection<Unit> _availableUnits;
private Unit _selectedUnit;
private Unit _previouslySelected;
#endregion Private Fields
#region Public Constructors
public MainVm()
{
_availableUnits = new ObservableCollection<Unit>()
{
new Unit("mm²"),
new Unit("cm²"),
new Unit("dm²"),
new Unit("m²")
};
}
#endregion Public Constructors
#region Public Properties
public double Value
{
get
{
return _value;
}
set
{
if (_value != value)
{
_value = value;
OnPropertyChanged();
}
}
}
public Unit SelectedUnit
{
get { return _selectedUnit; }
set
{
_previouslySelected = _selectedUnit;
_selectedUnit = value;
// call to value conversion function
// convert cm² to mm² or anything
Value = UnitConvertor.Convert(_value, _previouslySelected.Name, _selectedUnit.Name);
OnPropertyChanged();
}
}
public ObservableCollection<Unit> AvailableUnits => _availableUnits;
#endregion Public Properties
}
My Observable class will be like
public class Observable : INotifyPropertyChanged
{
#region Public Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion Public Events
#region Protected Methods
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion Protected Methods
}
better to use an enum for units

How do I use x:Bind function binding with a null parameter?

I am trying to format several properties of an object and bind the result to a TextBlock using x:Bind function binding. The binding looks like this:
<TextBlock Text="{x:Bind local:MainViewModel.FormatWidget(ViewModel.SelectedItem), Mode=OneWay}" />
As long as the object is not null, this works perfectly. However, when the object is null, my function is not called. Or to be more precise, if the object is null initially, the function is called, but if the object changes to null later, the function is not called.
Why is the function not being called when the parameter is null and how can I use it for this case?
Here's a repro. When you run it, notice that initially the function binds correctly to the null SelectedItem and displays "No widget selected." But when you select an item and then unselect it (CTRL + click to unselect), it does not call the function and displays the FallbackValue. (If the FallbackValue is not set, it does not update the binding at all.)
MainPage.xaml
<Page
x:Class="NullFunctionBindingParameter.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NullFunctionBindingParameter">
<Page.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="20" />
</Style>
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListView
Grid.Column="0"
ItemsSource="{x:Bind ViewModel.Widgets, Mode=OneWay}"
SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Widget">
<TextBlock Text="{x:Bind Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock Grid.Column="1" Text="{x:Bind local:MainViewModel.FormatWidget(ViewModel.SelectedItem), Mode=OneWay, FallbackValue=MyFallbackValue}" />
</Grid>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml.Controls;
namespace NullFunctionBindingParameter
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
}
public MainViewModel ViewModel { get; } = new MainViewModel();
}
}
MainViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace NullFunctionBindingParameter
{
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Widget _selectedItem;
public Widget SelectedItem
{
get => _selectedItem;
set
{
if (_selectedItem != value)
{
_selectedItem = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
}
}
}
public ObservableCollection<Widget> Widgets { get; } = new ObservableCollection<Widget>()
{
new Widget
{
Id = Guid.NewGuid(),
Name = "Regular Widget",
Model = "WX2020-01",
Description = "Your typical everyday widget."
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Super Widget",
Model = "WX2020-02",
Description = "An extra special upgraded widget."
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Broken Widget",
Model = "WX2020-03",
Description = "A widget that has been used and abused."
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Fake Widget",
Model = "WX2020-04",
Description = "It's not really a widget at all!"
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Surprise Widget",
Model = "WX2020-05",
Description = "What kind of widget will it be?"
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Invisible Widget",
Model = "WX2020-06",
Description = "Our most inexpensive widget."
},
new Widget
{
Id = Guid.NewGuid(),
Name = "Backwards Widget",
Model = "WX2020-07",
Description = "Really more of a tegdiw, come to think of it."
}
};
public static string FormatWidget(Widget widget)
{
if (widget == null)
return "No widget selected";
else
return $"{widget.Name} [{widget.Model}] {widget.Description}";
}
public string GetFormattedWidget()
{
return FormatWidget(SelectedItem);
}
}
}
Widget.cs
using System;
using System.ComponentModel;
namespace NullFunctionBindingParameter
{
public class Widget : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Guid _id;
private string _name;
private string _model;
private string _description;
public Guid Id
{
get => _id;
set
{
if (_id != value)
{
_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Id)));
}
}
}
public string Name
{
get => _name;
set
{
if (_name != value)
{
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
}
public string Model
{
get => _model;
set
{
if (_model != value)
{
_model = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Model)));
}
}
}
public string Description
{
get => _description;
set
{
if (_description != value)
{
_description = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Description)));
}
}
}
}
}
In this case, I recommend that you use Converter instead of using static methods directly in the binding statement.
WidgetConverter
public class WidgetConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var widget = value as Widget;
return MainViewModel.FormatWidget(widget);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Usage
<Page.Resources>
...
<local:WidgetConverter x:Key="WidgetConverter"/>
</Page.Resources>
...
<TextBlock
Grid.Row="2"
Grid.Column="2"
Text="{x:Bind ViewModel.SelectedItem, Mode=OneWay,Converter={StaticResource WidgetConverter}}"/>
Best regards.

How to bind to TextBox.Text in local ValidationRule class

This is my XAML file:
<Window x:Class="WpfListView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfListView"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView Margin="10" Name="lvUsers">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Margin="0,0,5,0">
<Binding Path="Mail" Mode="TwoWay">
<Binding.ValidationRules>
<local:NameValidation>
<local:NameValidation.Params>
<local:NameValidationParameters
OriginalTree="{Binding Source={x:Reference lvUsers}}"
OriginalName="{Binding RelativeSource={RelativeSource Self}}"/> <!-- I want the OriginalName to be TextBox.Text-->
</local:NameValidation.Params>
</local:NameValidation>
</Binding.ValidationRules>
</Binding>
</TextBox>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
This is my MainWindow class:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john#doe-family.com" });
items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane#doe-family.com" });
items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe#gmail.com" });
lvUsers.ItemsSource = items;
}
}
And this is my ValidationRule class:
public class NameValidationParameters : DependencyObject
{
public ListView OriginalTree
{
get { return (ListView)this.GetValue(OriginalTreeProperty); }
set { this.SetValue(OriginalTreeProperty, value); }
}
public string OriginalName
{
get { return (string)this.GetValue(OriginalNameProperty); }
set { this.SetValue(OriginalNameProperty, value); }
}
public static readonly DependencyProperty OriginalTreeProperty
= DependencyProperty.Register(nameof(OriginalTree), typeof(ListView),
typeof(NameValidationParameters));
public static readonly DependencyProperty OriginalNameProperty
= DependencyProperty.Register(nameof(OriginalName), typeof(string),
typeof(NameValidationParameters));
}
public class NameValidation : ValidationRule
{
public string ErrorMessage
{ get; set; }
public NameValidationParameters Params { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
ValidationResult lResult = null;
return lResult;
}
}
As you can see, when the NameValidation.Validate is called, I want NameValidation.Params to be populated with correct variables:
NameValidation.Params.OriginalTree should be the original ListView. This I can get.
NameValidation.Params.OriginalName should be the current TextBox's Text. In this case, it is bound to Mail, so I would expect to see that it is the email address like john#doe-family.com. However I can't get this out; all I can see is that NameValidation.Params.OriginalName is "WpfListView.NameValidationParameters"
Also, I want to be able to access the current index in the list inside the NameValidation.Validate method. How can I get it?
As mentioned elsewhere,
Validation rules do not hold or inherit a DataContext this will prevent binding from working as expected if you try to declare everything inline. Instead, declare your bindable rule options as a resource and set the property on your custom rule using a StaticBinding.
I've modified your TextBox to add a resource parameter:
<TextBox Grid.Column="0" Margin="0,0,5,0" x:Name="box1">
<TextBox.Resources>
<local:NameValidationParameters OriginalName="{Binding Mail}" OriginalTree="{Binding Source={x:Reference lvUsers}}" x:Key="Parameters"/>
</TextBox.Resources>
<TextBox.Text>
<Binding Path="Mail" Mode="TwoWay">
<Binding.ValidationRules>
<local:NameValidation Params="{StaticResource Parameters}"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
And NameValidationParameters to implement Freezable
public class NameValidationParameters : Freezable
{
public ListView OriginalTree
{
get { return (ListView)this.GetValue(OriginalTreeProperty); }
set { this.SetValue(OriginalTreeProperty, value); }
}
public string OriginalName
{
get { return (string)this.GetValue(OriginalNameProperty); }
set { this.SetValue(OriginalNameProperty, value); }
}
public static readonly DependencyProperty OriginalTreeProperty
= DependencyProperty.Register(nameof(OriginalTree), typeof(ListView),
typeof(NameValidationParameters));
public static readonly DependencyProperty OriginalNameProperty
= DependencyProperty.Register(nameof(OriginalName), typeof(object),
typeof(NameValidationParameters));
protected override Freezable CreateInstanceCore()
{
return new NameValidationParameters();
}
}
However, Here is an example of how i implemented INotifyDataErrorInfo in my project. This is a very clean way to trigger validation errors and xaml allows you to configure the display of errors
Model class:
public class AttributionInput
{
[Required]
public DateTime? StartDate { get; set; }
[Required]
public DateTime? EndDate { get; set; }
}
ModelWrapper Generic:
public class ModelWrapper<T> : NotifyDataErrorInfoBase
{
public T Model { get; protected set; }
public ModelWrapper(T model)
{
Model = model;
}
public ModelWrapper()
{
}
protected virtual TValue GetValue<TValue>([CallerMemberName] string propertyName = null)
{
return (TValue)typeof(T).GetProperty(propertyName)?.GetValue(Model);
}
protected virtual void SetValue<TValue>(TValue value, [CallerMemberName] string propertyName = null)
{
typeof(T).GetProperty(propertyName)?.SetValue(Model, value);
OnPropertyChanged(propertyName);
ValidatePropertyInternal(propertyName, value);
}
private void ValidatePropertyInternal(string propertyName, object currentValue)
{
ClearErrors(propertyName);
ValidateDataAnnotations(propertyName, currentValue);
ValidateCustomErrors(propertyName);
}
protected virtual IEnumerable<string> ValidateProperty(string propertyName)
{
return null;
}
private void ValidateCustomErrors(string propertyName)
{
var errors = ValidateProperty(propertyName);
if (errors != null)
{
foreach (var error in errors)
{
AddError(propertyName, error);
}
}
}
private void ValidateDataAnnotations(string propertyName, object currentValue)
{
var results = new List<ValidationResult>();
var context = new ValidationContext(Model) { MemberName = propertyName };
Validator.TryValidateProperty(currentValue, context, results);
foreach (var result in results)
{
AddError(propertyName, result.ErrorMessage);
}
}
}
Generic Implementation:
public class AttributionInputWrapper : ModelWrapper<AttributionInput>
{
public AttributionInputWrapper(AttributionInput model) : base(model)
{
}
public DateTime? StartDate
{
get => GetValue<DateTime?>();
set
{
SetValue(value);
if (EndDate < StartDate) EndDate = StartDate;
}
}
public DateTime? EndDate
{
get => GetValue<DateTime?>();
set
{
SetValue(value);
if (EndDate < StartDate) StartDate = EndDate;
}
}
protected override IEnumerable<string> ValidateProperty(string propertyName)
{
if (propertyName == nameof(EndDate) || propertyName == nameof(StartDate))
{
//if (StartDate.Value.Date > EndDate.Value.Date) yield return "Start Date must be <= End Date";
if (EndDate != null && (EndDate.Value.DayOfWeek == DayOfWeek.Saturday || EndDate.Value.DayOfWeek == DayOfWeek.Sunday))
yield return "Please select a week day";
if (StartDate != null && (StartDate.Value.DayOfWeek == DayOfWeek.Saturday || StartDate.Value.DayOfWeek == DayOfWeek.Sunday))
yield return "Please select a week day";
}
}
}
ViewModel:
public class QueryViewModel : DetailViewModelBase, ICommonViewModel
{
private AttributionInputWrapper _attributionInput;
public AttributionInputWrapper AttributionInput
{
get => _attributionInput;
set
{
_attributionInput = value;
OnPropertyChanged();
}
}
}
View :
<Style TargetType="DatePicker">
<Setter Property="Margin" Value="{StaticResource MarginString}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder x:Name="Placeholder1"/>
<TextBlock Text="{Binding ElementName=Placeholder1, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" FontSize="9"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<DatePicker x:Name="StartDateBox" SelectedDate="{Binding AttributionInput.StartDate, UpdateSourceTrigger=PropertyChanged}" DisplayDateStart="10/01/2017" DisplayDateEnd="{x:Static system:DateTime.Now}"/>

textbox not updating its value from viewmodel

I have a command which sends text when the send button is clicked. The binding is set to two way and the updatesource trigger to propertychanged. but the value of the textbox doesnt change to string.empty which is included in the sendCommand, even though the command was able to take the updated textbox value for a new message.
public class BuddyChatViewModel : BaseViewModel
{
private string chat;
public string Chat
{
get { return chat; }
set
{
chat = value;
RaisePropertyChanged();
}
}
public RelayCommand sendChatCommand { get; private set; }
string username = "";
string buddy = "";
UriStrings url = new UriStrings();
BuddiesHomeModel buddiesList = new BuddiesHomeModel();
HttpService http = new HttpService();
StorageService store = new StorageService();
string response = "";
BuddyChatModel buddyChat = new BuddyChatModel();
List<BuddyChat2Datum> buddychatList = new List<BuddyChat2Datum>();
BuddyChat2Datum tempDatum = new BuddyChat2Datum();
private ObservableCollection<BuddyChat2Datum> buddyChatOC = new ObservableCollection<BuddyChat2Datum>();
public ObservableCollection<BuddyChat2Datum> BuddyChatOC
{
get { return buddyChatOC; }
set
{
buddyChatOC = value;
RaisePropertyChanged();
}
}
private async void sendChatExecute()
{
int i = 0;
string s = url.buddychatText(username, buddy, chat);
chat = "";
response = await http.GetAsync(s);
buddyChat = JsonConvert.DeserializeObject<BuddyChatModel>(response);
buddychatList.Clear();
for (i = 0; i < buddyChat.data.Count; i++)
{
tempDatum.conversation = buddyChat.data[i].conversation;
tempDatum.datetime = buddyChat.data[i].datetime;
tempDatum.from = buddyChat.data[i].from;
tempDatum.to = buddyChat.data[i].to;
if (tempDatum.from == username)
tempDatum.isLeft = false;
else
tempDatum.isLeft = true;
buddychatList.Add(tempDatum);
tempDatum = new BuddyChat2Datum();
}
BuddyChatOC.Clear();
for (i = 0; i < buddychatList.Count; i++)
{
BuddyChatOC.Add(buddychatList[i]);
}
Navigate<BuddyChatViewModel>(buddychatList);
}
#region State Management
public override void LoadState(object navParameter, Dictionary<string, object> state)
{
sendChatCommand = new RelayCommand(sendChatExecute);
int i = 0;
base.LoadState(navParameter, state);
BuddyChatOC.Clear();
// load test items again; in production this would retrieve the live item by id or get it from a local data cache
List<BuddyChat2Datum> buddychatList = (List<BuddyChat2Datum>)navParameter;
//var mes = new MessageDialog(buddychatList.Count.ToString());
//await mes.ShowAsync();
for(i=0;i<buddychatList.Count;i++)
{
BuddyChatOC.Add(buddychatList[i]);
}
username = buddychatList[i-1].username;
buddy = buddychatList[i-1].buddy;
}
public override void SaveState(Dictionary<string, object> state)
{
base.SaveState(state);
}
#endregion
}
}
xaml code:
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<ListView x:Name="chatList" HorizontalAlignment="Stretch" ItemsSource="{Binding BuddyChatOC}" ItemTemplateSelector="{StaticResource ChatSelector}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
<RelativePanel Grid.Row="1" Margin="5,10,5,10">
<TextBox x:Name="sendtext" Margin="0,0,2,0" Text="{Binding Chat, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" RelativePanel.AlignLeftWithPanel="True" RelativePanel.LeftOf="sendtextbutton"/>
<Button x:Name="sendtextbutton" Content="Send" Command="{Binding sendChatCommand}" RelativePanel.AlignRightWithPanel="True" >
</Button>
</RelativePanel>
</Grid>
Implement INotifyPropertyChanged in BuddyChatViewModel.
public class BuddyChatViewModel : INotifyPropertyChanged, BaseViewModel
{
private string chat;
public string Chat
{
get { return chat; }
set
{
chat = value;
NotifyPropertyChanged("Chat");
}
}
//INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
If you're using MVVMLight (and from how this question is tagged I assume you do), you need to specify changed property name in RaisePropertyChanged call.
That should work in your case:
public string Chat
{
get { return chat; }
set
{
chat = value;
RaisePropertyChanged(() => Chat);
}
}

Auto bind data to grid.textblock

I have problem that i don't know how to bind data in windows phone 8 and the scenario is :-
I request data from server and after I got response I display them in grid and list boxes (like a table).
Until here every thing is okay after I finish the display function there is a new function that retrieves the update to last request.
Here is my problem:
When I receive the new data I don't know how bind this new data to same old table.
For example: the first request is return the gold price 1500$---> I display in table--->then new request--> the update function return the new price of gold is 1502$.
How to update desired row that have gold price textblock with the new price while the application running.
This the first request:-
public ObservableCollection<Data> DataReceivedCollection { get; set; }
private void FireRequest2()
{
var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/getallsymbols?AccountID=1122336675")) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookieJar;
request.BeginGetResponse(ar =>
{
HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)req2.EndGetResponse(ar))
{
using (Stream stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
var outerRoot1 = JsonConvert.DeserializeObject<OuterRootObject1>(reader.ReadToEnd());
JArray jsonArray = JArray.Parse(outerRoot1.d);
JToken jsonArray_Item = jsonArray.First;
while (jsonArray_Item != null)
{
string Name = jsonArray_Item.Value<string>("Name");
string Bid = jsonArray_Item.Value<string>("Bid");
string Ask = jsonArray_Item.Value<string>("Ask");
string ID = jsonArray_Item.Value<string>("ID");
DataReceivedCollection = new ObservableCollection<Data>();
DispatchInvoke(() =>
{
myList.ItemsSource = DataReceivedCollection;
// and to add data you do it like this:
DataReceivedCollection.Add(new Data() { symid = ID, textFirst = Name, textSecond = Bid, textThird = Ask });
}
);
//Be careful, you take the next from the current item, not from the JArray object.
jsonArray_Item = jsonArray_Item.Next;
}
}
}
}
}, request);
}
And here is my XAML that i want to dsiaply the requested data from firerequest2();
<Grid Background="#FFC9DC97" x:Name="ContentPanel" Grid.Row="1" Margin="12,140,12,0">
<ListBox Name="myList" Background="#FFC9DC97">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="ide" Text="{Binding symid}" Grid.Column="3" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding textFirst}" Grid.Column="0" HorizontalAlignment="Left" Foreground="#FF1C69D8"/>
<TextBlock Text="{Binding textSecond}" Grid.Column="1" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding textThird}" Grid.Column="2" HorizontalAlignment="Right"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
To here every thing is working fine i don't know to update the grid with the new data from the next function
public class Data : INotifyPropertyChanged
{
private string _textFirst;
public string textFirst
{
[DebuggerStepThrough]
get { return _textFirst; }
[DebuggerStepThrough]
set
{
if (value != _textFirst)
{
_textFirst = value;
OnPropertyChanged("textFirst");
}
}
}
private string _textSecond;
public string textSecond
{
[DebuggerStepThrough]
get { return _textSecond; }
[DebuggerStepThrough]
set
{
if (value != _textSecond)
{
_textSecond = value;
OnPropertyChanged("textSecond");
}
}
}
private string _textThird;
public string textThird
{
[DebuggerStepThrough]
get { return _textThird; }
[DebuggerStepThrough]
set
{
if (value != _textThird)
{
_textThird = value;
OnPropertyChanged("textThird");
}
}
}
private string _symid;
public string symid
{
[DebuggerStepThrough]
get { return _symid; }
[DebuggerStepThrough]
set
{
if (value != _symid)
{
_symid = value;
OnPropertyChanged("symid");
}
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
Your dataReceived collection needs to be declared like this because it is the subject of a binding...
public ObservableCollection<Data> DataReceivedCollection { get; set; }
And in the initialization code, it needs to be instantiated like this...
DataReceivedCollection = new ObservableCollection<Data>();
And your data class should be declared something like this (not all properties declared)
public class Data : INotifyPropertyChanged
{
private string _textFirst;
public string TextFirst
{
[DebuggerStepThrough]
get { return _textFirst; }
[DebuggerStepThrough]
set
{
if (value != _textFirst)
{
_textFirst = value;
OnPropertyChanged("TextFirst");
}
}
}
private string _textSecond;
public string TextSecond
{
[DebuggerStepThrough]
get { return _textSecond; }
[DebuggerStepThrough]
set
{
if (value != _textSecond)
{
_textSecond = value;
OnPropertyChanged("TextSecond");
}
}
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
Doing these things will ensure that the binding engine gets the information it needs to populate your List Box.
This is only a start that will give you some better results. As mentioned in the commentary, your next port of call is to take up a study of INotifyPropertyChanged.

Categories

Resources