-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MVVM Binding OnLabel isn't working #1867
Comments
Can you provide both the XAML and the ViewModel wire-up? |
ViewModel + Model: public class DemoViewModel : ViewModelBase
{
#region -- Fields --
private Model model = null;
#endregion
#region -- Properties --
public Model Model
{
get { return this.model; }
set
{
this.model = value;
RaisePropertyChanged("Model");
}
}
#endregion
#region -- Commands --
public RelayCommand GoCommand { get; set; }
#endregion
public DemoViewModel()
{
this.GoCommand = new RelayCommand(Initialize);
}
protected void Initialize()
{
var model = new Model();
model.Initialize();
this.Model = model;
}
}
public class Model : ObservableObject
{
#region -- Fields --
private bool isChecked = false;
private string label = string.Empty;
#endregion
#region -- Properties --
public bool IsChecked
{
get { return this.isChecked; }
set
{
this.Label = value ? "1234" : string.Empty;
this.isChecked = value;
RaisePropertyChanged("IsChecked");
}
}
public string Label
{
get { return this.label; }
set
{
this.label = value;
RaisePropertyChanged("Label");
}
}
#endregion
public void Initialize()
{
this.Label = "4321";
this.isChecked = !string.IsNullOrWhiteSpace(this.Label);
}
} View <Window x:Class="WpfScratch.DemoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DemoWindow" Height="300" Width="300"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
DataContext="{Binding DemoViewModel, Source={StaticResource Locator}}">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Controls:ToggleSwitch Width="110" IsChecked="{Binding Model.IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" OnLabel="{Binding Model.Label}" OffLabel="" />
<Button Content="Go" Command="{Binding GoCommand}" />
</StackPanel>
</Grid>
</Window> The only way I can work around this is for me to replace the control's OnLabel="{Binding}" with Style="{DynamicResource LabelToggleSwitch}" and then add the style in my Window.Resources --- <Style x:Key="LabelToggleSwitch" TargetType="{x:Type Controls:ToggleSwitch}" BasedOn="{StaticResource {x:Type Controls:ToggleSwitch}}">
<Setter Property="OnLabel" Value="{Binding Label, UpdateSourceTrigger=PropertyChanged}" />
</Style> |
I believe the issue you're seeing is because you're calling Basically, your Model creates an INotifyPropertyChange event in the Initialize method, but there's nothing listening for it yet. So the view never gets the message to update to 4321. Try calling |
@TravyDale did this resolve your issue? If so, can you mark issue as closed? |
@killnine I do have the same issue. Changing my localization only get's reflected in the On/Off labels after toggling the switch. Works with all other controls on my view. |
So I am using MVVM Light and I am binding my OnLabel to a string property I have in my ViewModel. This works only when I have the toggle NOT checked. Once I check it, the OnLabel goes to the correct string. If I have the toggle initially checked, the OnLabel is always 'On' and not whatever my string is inside of my ViewModel. If I uncheck the toggle, then check it, the correct string shows. Any thoughts?
The text was updated successfully, but these errors were encountered: