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>
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.
This question already has answers here:
WPF Binding without path with converter, update
(3 answers)
Closed 5 years ago.
I have a binding:
<TextBlock Text="{Binding Converter={local:Converter}}" />
with converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var vm = (ViewModel)value;
return vm.SomeProperty;
}
Changing SomeProperty doesn't update the value on screen. Converter is not called.
How do I update such binding from within view model?
Note: in real project converter will be used to perform some calculation and returning result. In fact it will even be MultiBinding with similar bindings to different view models. I had problem with it and was able to narrow down the case to a simple Binding, where binding is not used to bind to a property, but like this.
MCVE xaml:
<StackPanel>
<TextBlock Text="{Binding Converter={local:Converter}}" />
<Button Content="..." Click="Button_Click" />
</StackPanel>
and code:
public partial class MainWindow : Window
{
ViewModel _vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _vm;
}
void Button_Click(object sender, RoutedEventArgs e) => _vm.SomeProperty += "b";
}
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
string _someProperty = "a";
public string SomeProperty
{
get { return _someProperty; }
set
{
_someProperty = value;
OnPropertyChanged(nameof(SomeProperty));
}
}
}
public class Converter : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider) => this;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var vm = (ViewModel)value;
return vm.SomeProperty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
if the ViewModel is set to DataContext of the View. You can simply bind the Property by using <TextBlock Text="{Binding SomeProperty}" />
From the comments, if you would bind the whole viewModel, because the converter do more stuff, than only convert type, or some thing. have a look at this: answer
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.
I have a boolean property (that does called INotifyPropertyChanged in the setter) that is bound to a button.IsEnabled property in my XAML. Currently I'm using a TwoWay binding, but this is causing problems and I only need a OneWay binding. My problem is that the converter I'm using doesn't get called beyond the first time the program starts up. I've put breakpoints in the setter and it gets called loads, but the Convert() method doesn't get called at all. Why is this?
Some code:
public bool IsSaving
{
get
{
return _isSaving;
}
set
{
_isSaving = value;
NotifyOfPropertyChange(() => IsSaving);
}
}
and the XAML:
IsEnabled="{Binding Path=IsSaving, Mode=OneWay, Converter={StaticResource booleanToNotEnabledConverter}}"
The converter really just returns !(bool)value so the button gets disabled when IsSaving is true.
Some changes at runtime might cause the binding to break (since you bind to the DataContext + a relative path), if you use Visual Studio make sure to check the Output-window for any binding errors.
Edit: Since it has not been noted: That is a stardard binding and there is nothing wrong with the posted code, the problem has to be caused by the context.
Here is the code I used and this works:
Converter:
using System.Windows.Data;
using System;
namespace SilverlightApplication1
{
public class BooleanToNotEnabledConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
XAML:
<UserControl x:Class="SilverlightApplication1.MainPage"
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:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<local:BooleanToNotEnabledConverter x:Key="booleanToNotEnabledConverter" />
</UserControl.Resources>
<StackPanel Margin="100">
<Button Content="Flip"
Click="Button_Click" />
<TextBlock Text="{Binding IsSaving}"
Height="20" />
<Button IsEnabled="{Binding IsSaving, Mode=OneWay, Converter={StaticResource booleanToNotEnabledConverter}}"
Content="Some Button" />
</StackPanel>
</UserControl>
Code behind:
using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
private Data _data;
public MainPage()
{
InitializeComponent();
_data = new Data { IsSaving = true };
this.DataContext = _data;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_data.IsSaving = !_data.IsSaving;
}
}
public class Data : INotifyPropertyChanged
{
#region IsSaving Property
private bool _isSaving;
public bool IsSaving
{
get
{
return _isSaving;
}
set
{
if (_isSaving != value)
{
_isSaving = value;
OnPropertyChanged("IsSaving");
}
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var p = PropertyChanged;
if (p != null)
{
p(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Are you sure you invoke the PropertyChanged event handler with the correct string?
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsSaving"));