In the following DataTemplate, the first binding doesn't work while the 2nd one works, and I would like to know why.
<local:IsEnabledConverter x:Key="isEnabled"/>
<local:Boolean2TextConverter x:Key="txtConverter"/>
<DataTemplate x:Key="fileinfoTemplate" DataType="{x:Type local:MyFileInfo}">
<StackPanel>
<Label x:Name="1stLabel" Content="{Binding Path=Filename}" IsEnabled="{Binding Path=., Converter={StaticResource isEnabled}}"/> <--- doesn't work
<Label x:Name="2ndLabel" Content="{Binding Path=IfPrint, Converter={StaticResource txtConverter}}" IsEnabled="{Binding Path=IsChecked, ElementName=ckBox}"/> <--- works
<CheckBox x:Name="ckBox" IsChecked="{Binding Path=IfPrint}" IsEnabled="{Binding Path=IsValid}" Style="{StaticResource printCkBox}"/>
</StackPanel>
</DataTemplate>
IsEnabledConverter:
class IsEnabledConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
MyFileInfo f = value as MyFileInfo;
return f.IsValid && f.IfPrint;
}
//... omit ConvertBack NotImplementedException stuff
}
Boolean2TextConverter:
class IsEnabledConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
Boolean b = (Boolean)value;
return b.ToString();
}
//similarly omit ConvertBack here
}
Code for MyFileInfo:
public class MyFileInfo {
public string IfPrint {
get;
set;
}
public string IsValid {
get;
set;
}
...
}
Problem: When the CheckBox is toggled, the 2nd Label grays out and shows "false", or becomes normal and shows "true", as it should. However, the first Label doesn't change at all; its IsEnabled state is supposed be the conjunction of two Booleans, one of which is changed by the CheckBox. What is wrong? (note that the IsEnabledConverter is called once upon GUI initialization, but not called again when its binding source changes.)
There are 2 issues here. First you have to implement INotifyPropertyChanged for the ViewModel MyFileInfo. Secondly you have to use MultiBinding here. Because I don't think we have some way to trigger updating the target (such as when toggling the CheckBox) if you bind the whole view model to the IsEnabled target. So here is how it should be done:
Your view model:
public class MyFileInfo : INotifyPropertyChanged {
bool _ifPrint;
bool _isValid;
public bool IfPrint {
get { return _ifPrint; }
set {
if(_ifPrint != value) {
_ifPrint = value;
OnPropertyChanged("IfPrint");
}
}
}
public bool IsValid {
get { return _isValid; }
set {
if(_isValid != value) {
_isValid = value;
OnPropertyChanged("IsValid");
}
}
}
//Implement INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string prop){
var handler = PropertyChanged;
if(handler != null) handler(this, new PropertyChangedEventArgs(prop));
}
//.... should do the same for the remaining properties....
//...
}
Here is the converter used for MultiBinding, which should implement IMultiValueConverter (instead of IValueConverter):
class IsEnabledConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture) {
if(values.Length == 2){
return (bool) values[0] && (bool) values[1];
}
return false;
}
//... omit ConvertBack NotImplementedException stuff
}
Here is the modifed XAML (to use MultiBinding instead):
<Label x:Name="firstLabel" Content="{Binding Path=Filename}">
<Label.IsEnabled>
<MultiBinding Converter="{StaticResource isEnabled}">
<Binding Path="IsValid"/>
<Binding Path="IfPrint"/>
</MultiBinding>
</Label.IsEnabled>
</Label>
Now one of IsValid and IfPrint changing will trigger the MultiBinding's Converter. Here you can also bind to IsChecked of the CheckBox directly instead of indirectly via IfPrint.
PS: Note Name used in XAML (as well as in codebehind) must not start with number.
Since the instance of MyFileInfo does not change while you check/uncheck the checkbox hence IsEnabledConverteris not getting called.
In order to Enable/Disable your 1stLabel depending on two properties, either use MultiValueConverter or use MultiDataTrigger by applying Style to your Label.
Related
how can i solve this using MVVM pattern, and I am using Devexpress MVVM. I have many textbox in form.
And i need to set the textbox text into "[blank]" when the user press the Ctrl+B and the current text of the textbox is null or ""
But i am looking for a way to use the IValueConverter if possible
I have a class similar to this
public class BlankText : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (string.IsNullOrEmpty(value.ToString()))
return "[blank]";
else
return value;
}
}
And i have this code in the resources
<UserControl.Resources>
<c:BlankText x:Key="BlankText"/>
</UserControl.Resources>
This is my TextBox
<TextBox Text="{Binding District}" >
<TextBox.InputBindings>
<KeyBinding Gesture="Ctrl+B">
</KeyBinding>
</TextBox.InputBindings>
</TextBox>
But my problem is how can i call it on keypress? Am i doing it right?
In order to perform actions using KeyBinding, you can't use IValueConverter. IValueConverters are for converting values, not performing actions. What you need is to define a class that implements ICommand, and then assign an instance of that class to KeyBinding.Command.
public class BlankCommand : ICommand
{
public MyViewModel ViewModel { get; }
public BlankCommand(MyViewModel vm)
{
this.ViewModel = vm;
}
public void Execute(object parameter)
{
// parameter is the name of the property to modify
var type = ViewModel.GetType();
var prop = type.GetProperty(parameter as string);
var value = prop.GetValue(ViewModel);
if(string.IsNullOrEmpty(value))
prop.SetValue(ViewModel, "[blank]");
}
public boolean CanExecute(object parameter) => true;
public event EventHandler CanExecuteChanged;
}
Then create an instance of this class and attach it to your ViewModel so that the KeyBinding can access it:
<TextBox Text="{Binding District}">
<TextBox.InputBindings>
<KeyBinding Gesture="Ctrl+B" Command="{Binding MyBlankCommand}" CommandParameter="District"/>
</TextBox.InputBindings>
</TextBox>
Changing the text to say "[blank]" when the user presses a keyboard shortcut is a weird UX pattern, however. I would suggest adding a placeholder to the text box instead.
I have a standard Enum that will either be Yes or No:
public enum YesOrNo
{
Yes,
No
}
My base Model Class has a YesOrNo property like this:
public class Group : NotifyPropertyChanged
{
private YesOrNo groupOperator;
public YesOrNo GroupOperator
{
get
{
return this.groupOperator;
}
set
{
this.groupOperator = value;
OnPropertyChanged("GroupOperator");
}
}
In my View, I am using a ToggleSwitch, similar to a slider you would see on a Mobile phone. Sliding back and forth should effectively reassign the value of the Enum. So it will default as Yes and sliding the toggle will set the Enum value to No and alternatively.
If I were to have a test method that reassigns the Enum when the Checked command is hit, the PropertyChanged event is fired so I know that is technically working. I am just wondering how I could go about alternating values in the Enum.
This is the ToggleButton in my XAML:
<ToggleButton Style="{StaticResource ToggleViewSwitch}" Command="{Binding SetOperatorCommand, UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"/>
And this is my Main View Model, where I hold the Command and the test method to assign the value manually:
private bool isChecked = false;
public bool IsChecked
{
get
{
return this.isChecked;
}
set
{
this.isChecked = value;
OnPropertyChanged("IsChecked");
}
}
private RelayCommand setOperatorCommand;
public ICommand SetOperatorCommand
{
get
{
if (this.setOperatorCommand == null)
{
this.setOperatorCommand = new RelayCommand(
x => ToggleGroupOperator());
}
return this.setOperatorCommand;
}
}
private void ToggleGroupOperator()
{
if (IsChecked)
{
TopLevelGroup.GroupOperator = YesNo.No;
}
else
{
TopLevelGroup.GroupOperator = YesNo.Yes;
}
}
First make a Converter...
public class YesOrNoToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> (value is YesOrNo yesOrNo && yesOrNo == YesOrNo.Yes) ? true : false;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> (value is bool isYes && isYes) ? YesOrNo.Yes : YesOrNo.No;
}
Then reference the converter during binding...
<Window.Resources>
<Converters:YesOrNoToBooleanConverter x:Key="YesOrNoToBooleanConverter" />
</Window.Resources>
<Grid>
<CheckBox IsChecked="{Binding GroupOperator, Converter={StaticResource YesOrNoToBooleanConverter}}" />
</Grid>
This will allow your ViewModel to remain using the enum without any overhead and the view to bind without any overhead; leave this binding manipulation work to converters.
I think I've followed the examples given in this post but my property is not changing when button are changed. Any suggestions on where I went wrong?
C# code for enum and class
public enum SystemTypes
{
TypeA,
TypeB
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SystemTypes systemType = SystemTypes.TypeA;
public SystemTypes SystemType
{
get { return systemType; }
set { systemType = value; }
}
}
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
xaml
<Canvas>
<Canvas.Resources>
<local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" />
</Canvas.Resources>
<RadioButton x:Name="TypeARadioButton" Content="TypeA" Canvas.Left="10" Canvas.Top="10"
IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeA}}" />
<RadioButton x:Name="TypeBRadioButton" Content="TypeB" Canvas.Left="10" Canvas.Top="31"
IsChecked="{Binding Path=SystemType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:SystemTypes.TypeB}}" />
</Canvas>
You need to set Binding Mode to TwoWay, then in Converter implement method ConvertBack responsible for converting bool to SystemTypes, in settter of SystemType include
set { systemType = value; OnPropertyChanged(() => "SystemType");}
in order to fill property in that its value was changed.
OnPropertyChanged(() => "SystemType")
can work if you implement interface INotifyPropertyChanged. I cannot you whether you set DataContext, if you did not binding is not working. In order to rectify this after InitializeComponent() add
this.DataContext = this;
I have a Listbox bound to an ObservableCollection of ImageMetadata class. Item template of Listbox is defined as
<Image Source="{Binding Converter={StaticResource ImageConverter}}" />
ImageConverter is written as
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var metadata = (ImageMetadata)value;
if (metadata.IsPublic)
{
//code to return the image from path
}
else
{
//return default image
}
}
ImageMetadata is the 'Model' class written as
class ImageMetadata : INotifyPropertyChanged
{
public string ImagePath
{
......
}
public bool IsPublic
{
......
}
}
When an image is updated, I will trigger PropertyChanged event as given below
NotifyPropertyChanged("ImagePath");
Problem here is that : NotifyPropertyChanged event will not work since I am specifying the changed property name as 'ImagePath' and the binding is to 'ImageMetadata' object rather than 'ImagePath' property.
I cannot use
<Image Source="{Binding ImagePath, Converter={StaticResource ImageConverter}}" />
since I need the IsPublic property also to decide which image to display.
How can I modify the code to properly fire PropertyChanged event?
Edit : I am developing for Windows phone 8.
You could use a MultiBinding with a multi-value converter:
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource ImageConverter}">
<Binding Path="ImagePath"/>
<Binding Path="IsPublic"/>
</MultiBinding>
</Image.Source>
</Image>
The Convert method would look like this:
public object Convert(
object[] values, Type targetType, object parameter,CultureInfo culture)
{
object result = null;
if (values.Length == 2 && values[0] is string && values[1] is bool)
{
var imagePath = (string)values[0];
var isPublic = (bool)values[1];
...
}
return result;
}
Let's say I have a ribbon tab name A (name="_tabA") and B (name="_tabB").
How can I disable or hide tab A or B dynamically?
I use VS2010 with RibbonControlsLibrary.dll.
<ribbon:RibbonTab Visibility="{Binding ShowThisRibbonTab, Converter=...}">
Where ShowThisRibbonTab is a property of your ViewModel and the Converter is most likely a BooleanToVisibilityConverter.
Alternatively, if you're not doing MVVM, you can just give it a name and set the Visibility
Without MVVM
I could easily hide/show with _tabA.Visibility = Visibility.Collapsed or Visibility.Visible.
With MVVM
The .xaml.cs code
Make the class inherit also from INotifyProperty
Make a property to raise event when property is modified
Setup DataContext.
Make Converter code.
The main code is as follows
public partial class MainWindow : RibbonWindow, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public const string NamePropertyName = "VisibleA";
private bool _visibleA = true;
public bool VisibleA
{
get
{
return _visibleA;
}
set
{
_visibleA = value;
RaisePropertyChanged(NamePropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The converter code is as follows
[ValueConversion(typeof(bool), typeof(Visibility))]
internal class CheckVisibleA : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? val = value as bool?;
string param = parameter as string;
if (value != null)
{
if (val == true)
{
return Visibility.Visible;
}
}
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
The .xaml code
This XAML has two ribbon tabs: _ribboHome and _ribbonHelp. And the property of "VisibleA" controls the visibility. When I click the checkbox, the VisibleA property turns on/off, and the _ribbonHome is visbile/collapsed accordingly.
<ribbon:Ribbon DockPanel.Dock="Top" Title="teusje.wordpress.com" >
<ribbon:RibbonTab Header="Home" Name="_ribbonHome" Visibility="{Binding Path=VisibleA, Converter={StaticResource CheckVisibleA}, ConverterParameter=Show}">
<ribbon:RibbonGroup Name="Clipboard" Header="Clipboard">
<ribbon:RibbonButton Command="{StaticResource hwc}" CommandParameter="Hello, smcho" Label="Copy" LargeImageSource="Images/LargeIcon.png" />
</ribbon:RibbonGroup>
</ribbon:RibbonTab>
<ribbon:RibbonTab Header="Help">
<ribbon:RibbonGroup Name="_ribbonHelp" Header="Help this">
<ribbon:RibbonButton Command="{StaticResource hwc}" CommandParameter="Hello, smcho" Label="Copy Help" LargeImageSource="Images/LargeIcon.png"/>
<ribbon:RibbonCheckBox IsChecked="{Binding Path=VisibleA}"/>
</ribbon:RibbonGroup>
</ribbon:RibbonTab>