XAML using radio buttons to enable and disable textbox with databinding - c#

I am trying to figure out how to enable and disable textboxes with radio buttons and data binding. It seems like I should be able to bind the textbox IsEnabled to a boolean and modify that value but I can't quite get it to work. I want to have several sets of radio buttons and textboxes so I want a generic way to handle the problem. I tried to use a converter but since I am not using any x:Names I am not sure those would be helpful in this case. I can get them to enable/disable the radio buttons but that is not what I want to do. My code shows mostly the solution I am trying for the first textbox.
Xaml Code
<Grid>
<StackPanel>
<RadioButton GroupName="grp1" Content="Enable TextBox" IsEnabled="{Binding Bttn1, Mode=TwoWay}" IsChecked="{Binding Bttn1}" Checked="RadioButton_Checked" Unchecked="RadioButton_UnChecked" />
<RadioButton GroupName="grp1" Content="Disable TextBox" IsEnabled="{Binding Bttn2}" />
<TextBox IsEnabled="{Binding Txtbx1, Mode=TwoWay}" BindingGroup="{Binding grp1}" ></TextBox>
<RadioButton GroupName="grp2" Content="Enable TextBox" IsEnabled="{Binding Bttn3, Mode=TwoWay}" IsChecked="{Binding Bttn3}" Checked="RadioButton_Checked" Unchecked="RadioButton_UnChecked" />
<RadioButton GroupName="grp2" Content="Disable TextBox" IsEnabled="{Binding Bttn4}" />
<TextBox IsEnabled="{Binding Txtbx2, Mode=TwoWay}" BindingGroup="{Binding grp2}" ></TextBox>
</StackPanel>
</Grid>
ViewModel Code
private bool _bttn1;
private bool _bttn2;
private bool _bttn3;
private bool _bttn4;
private bool _txtbx1;
private bool _txtbx2;
public bool Bttn1
{
get
{
return(_bttn1);
}
set
{
_bttn1 = value;
_txtbx1 = false;
RaisePropertyChanged(Txtbx1.ToString());
}
}
public bool Bttn2
{
get
{
return (_bttn1);
}
set
{
_bttn1 = value;
_txtbx1 = false;
RaisePropertyChanged(Txtbx1.ToString());
}
}
public bool Txtbx1
{
get
{
return (_txtbx1);
}
set
{
_txtbx1 = false;
}
}
Further down I have
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Handle(sender as RadioButton);
}
private void RadioButton_UnChecked(object sender, RoutedEventArgs e)
{
Handle(sender as RadioButton);
}
void Handle(RadioButton radioButton)
{
bool flag = radioButton.IsChecked.Value;
this.Title = "IsChecked = " + flag.ToString();
}
public class RadioConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)parameter;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value ? parameter : null;
}
}

In case you want to enable and disable textboxes with radio buttons, this solution works:
<Grid>
<StackPanel>
<RadioButton x:Name="RB1" GroupName="grp1" Content="Enable TextBox" />
<RadioButton GroupName="grp1" Content="Disable TextBox" />
<TextBox IsEnabled="{Binding IsChecked, ElementName=RB1}" ></TextBox>
<RadioButton x:Name="RB2" GroupName="grp2" Content="Enable TextBox" />
<RadioButton GroupName="grp2" Content="Disable TextBox" />
<TextBox IsEnabled="{Binding IsChecked, ElementName=RB2}" ></TextBox>
</StackPanel>

Related

Pass value UserControl to ViewModel of View Page

Here is the complete Sample code
Using MVVM pattern My requirement is to have a ListView where
If user Taps inside ListView on checkBox Storyboard Animation should play for True False and the ListView binded value should be updated in database. For true the tick should pop up with animation for false the tick should become invisible with animation. Solved as per #Elvis Xia answer
If user taps on ListviewItem Navigate to new page with value
Blueprint
Now I went with Usercontrol creation for the datatemplate. Here I want to identify both events separately user clicking on checkbox or clicking on Item separately. Using ICommand I am creating two Delegates that gets binded to two transparent button which relays tapped event. Dependency of creating transparent buttons and creating delgates while binding them made me think surely there must a better way in which I can utilize MVVM for these events without any code behind.
UserControl XAML
<Button Background="LightBlue" BorderBrush="White" BorderThickness="4" Command="{x:Bind sampleItem.itemTapped}" CommandParameter="{Binding}" HorizontalContentAlignment="Stretch">
<Grid Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="20" HorizontalAlignment="Center" Text="{x:Bind sampleItem.sampleText}" FontSize="30"/>
<Image Grid.Column="1" Height="60" Width="60" Source="ms-appx:///Assets/check_off.png" HorizontalAlignment="Right"/>
<Image x:Name="image" Grid.Column="1" Height="60" Width="60" Source="ms-appx:///Assets/check_on.png" HorizontalAlignment="Right" Visibility="Collapsed" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<CompositeTransform/>
</Image.RenderTransform>
</Image>
<Button x:Name="btnFav" Grid.Column="1" Height="60" Width="60" HorizontalAlignment="Right" Background="Transparent" Command="{x:Bind sampleItem.favTapped}" CommandParameter="{Binding}">
<Interactivity:Interaction.Behaviors>
<!--<Core:EventTriggerBehavior EventName="Tapped">
<Core:InvokeCommandAction Command="{Binding favTapped}" />
</Core:EventTriggerBehavior>-->
<Core:DataTriggerBehavior Binding="{Binding isFav}" Value="true">
<Media:ControlStoryboardAction Storyboard="{StaticResource StoryboardCheckOn}"/>
</Core:DataTriggerBehavior>
<Core:DataTriggerBehavior Binding="{Binding isFav}" Value="false">
<Media:ControlStoryboardAction Storyboard="{StaticResource StoryboardCheckOff}"/>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</Grid>
</Button>
UserControl XAML codeBehind
MainPageModel sampleItem { get { return this.DataContext as MainPageModel; } }
public MainPageUserControl()
{
this.InitializeComponent();
this.DataContextChanged += (s, e) => this.Bindings.Update();
}
Viewmodel Code
public async Task GetData()
{
for (int i = 0; i < 10; i++)
{
if (i == 3)
sampleList.Add(new MainPageModel { sampleText = "Selected", isFav = true, favTapped= new DelegateCommand<MainPageModel>(this.OnFavTapped), itemTapped= new DelegateCommand<MainPageModel>(this.OnItemTapped)});
else
sampleList.Add(new MainPageModel { sampleText = "UnSelected"+i.ToString(), isFav = null, favTapped = new DelegateCommand<MainPageModel>(this.OnFavTapped), itemTapped = new DelegateCommand<MainPageModel>(this.OnItemTapped) });
}
}
private void OnFavTapped(MainPageModel arg)
{
if (arg.isFav == null) arg.isFav = true;
else
arg.isFav = !arg.isFav;
}
private void OnItemTapped(MainPageModel arg)
{
System.Diagnostics.Debug.WriteLine("Button Value: "+arg.sampleText);
System.Diagnostics.Debug.WriteLine("Selected Item Value: "+selectedItem.sampleText);
}
MainPage Xaml
<Grid Grid.Row="1">
<ListView ItemsSource="{x:Bind ViewModel.sampleList}" IsItemClickEnabled="True" SelectedItem="{Binding ViewModel.selectedItem,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<userControls:MainPageUserControl/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
There must be a better way to achieve the desired result using code behind.
public class ViewMOdel()
{
public ViewModel()
{
favTapped= new DelegateCommand<MainPageModel>(this.OnFavTapped)
itemTapped= new DelegateCommand<MainPageModel>(this.OnItemTapped)
}
public async Task GetData()
{
for (int i = 0; i < 10; i++)
{
if (i == 3)
sampleList.Add(new MainPageModel { sampleText = "Selected", isFav = true});
else
sampleList.Add(new MainPageModel { sampleText = "UnSelected"+i.ToString(), isFav = null});
}
}
private void OnFavTapped(MainPageModel arg)
{
if (arg.isFav == null) arg.isFav = true;
else
arg.isFav = !arg.isFav;
}
private void OnItemTapped(MainPageModel arg)
{
System.Diagnostics.Debug.WriteLine("Button Value: "+arg.sampleText);
System.Diagnostics.Debug.WriteLine("Selected Item Value: "+selectedItem.sampleText);
}
}
Binding should be like this
<Button x:Name="btnFav" Grid.Column="1" Height="60" Width="60" HorizontalAlignment="Right" Background="Transparent" Command="{Binding ElementName=UserControl, Path=Tag.favTapped}" CommandParameter="{Binding}"/>
Update
<ListView ItemsSource="{x:Bind ViewModel.sampleList}" x:Name="Listview"IsItemClickEnabled="True" SelectedItem="{Binding ViewModel.selectedItem,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<userControls:MainPageUserControl Tag="{Binding DataContext,ElementName=Listview}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="btnFav" Grid.Column="1" Height="60" Width="60" HorizontalAlignment="Right" Background="Transparent" Command="{Binding ElementName=UserControl, Path=Tag.favTapped}" CommandParameter="{Binding}"/>
Update2 using EventTriggerBehavior
favTapped = new DelegateCommand<RoutedEventArgs>(this.OnFavTapped);
private void OnFavTapped(RoutedEventArgs arg)
{
var item = (( arg.OriginalSource )as Button).DataContext as MainPageModel
}
<Button n x:Name="btnFav" Grid.Column="1" Height="60" Width="60" HorizontalAlignment="Right" Background="Transparent" >
<interact:Interaction.Behaviors>
<interactcore:EventTriggerBehavior EventName="Click" >
<interactcore:InvokeCommandAction Command="{Binding ElementName=usercontrol, Path=Tag.favTapped}" />
</interactcore:EventTriggerBehavior>
</interact:Interaction.Behaviors>
</Button>
The DataContext of every item in your project is an instance of MainPageModel class. So the favTapped command should be added to MainPageModel class. And it is a command, so favTapped should be an instance of a new class,which implements ICommand interface.
And if you don't want the animation to show at the page's first load, you can set isFav to bool?. And when the page first loads, set the value of isFav to null, thus it won't trigger the animation action.
Below are the Codes snippets and Demo Link:
ViewModelCommands.cs:
public class ViewModelCommands : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
//if it's a tapped event
if (parameter is TappedRoutedEventArgs)
{
var tappedEvent = (TappedRoutedEventArgs)parameter;
var gridSource = (Grid)tappedEvent.OriginalSource;
var dataContext = (MainPageModel)gridSource.DataContext;
//if tick is true then set to false, or the opposite.
if (dataContext.isFav == null)
{
dataContext.isFav = true;
} else
{
dataContext.isFav = !dataContext.isFav;
}
}
}
}
MainPageModel.cs:
public class MainPageModel:BindableBase
{
public MainPageModel() {
favTapped = new ViewModelCommands();
}
public ViewModelCommands favTapped { get; set; }
private string _sampleText;
public string sampleText
{
get
{
return this._sampleText;
}
set
{
Set(ref _sampleText, value);
}
}
private bool? _isFav;
public bool? isFav
{
get
{
return this._isFav;
}
set
{
Set(ref _isFav, value);
}
}
}
Here is the complete Demo:Demo Project
Update:
When using DelegateCommand, you can add the command Property to MainPageModel.cs and since the DataContext of the items are MainPageModel instances. You can use this.isFav to change the clicked item's value of isFav.
Here are the codes of MainPageModel.cs:
public class MainPageModel : BindableBase
{
private DelegateCommand _favTapped;
public DelegateCommand favTapped
{
get
{
if (_favTapped == null)
{
_favTapped = new DelegateCommand(() =>
{
//Here implements the check on or off logic
this.CheckOnOff();
}, () => true);
}
return _favTapped;
}
set { _favTapped = value; }
}
private void CheckOnOff()
{
if (this.isFav == null)
{
this.isFav = true;
}
else
{
this.isFav = !this.isFav;
}
}
private string _sampleText;
public string sampleText
{
get
{
return this._sampleText;
}
set
{
Set(ref _sampleText, value);
}
}
private bool? _isFav;
public bool? isFav
{
get
{
return this._isFav;
}
set
{
Set(ref _isFav, value);
}
}
}
For Listview item selected
You can use ListView.ItemClick Event. But you should also set IsItemClickEnabled="True",otherwise the event handler won't be fired.
For The subitem of Listview tapped
You can use Tapped Event of userControl.
Here are the Xaml codes, that shows how to register the above two events:
<Grid Grid.Row="1">
<ListView IsItemClickEnabled="True" ItemClick="ListView_ItemClick_1" ItemsSource="{x:Bind ViewModel.sampleList}">
<ListView.ItemTemplate>
<DataTemplate>
<userControls:MainPageUserControl Tapped="MainPageUserControl_Tapped"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

Issue using a ObservableCollection from a different window

When the user wants to add a new Reminder, they click the add button on the mainWindow; and once they have added the data, it should display it in a listbox on the main window using an observable collection.
This brings up a new window which brings up options of, at the moment Date and message.
When the user has entered the data, Finish method is called.
The issue is, when the user has finished inputting the data on the new window, I add it to the reminder collection, but it doesn't update on the main window. I am wondering if is a datacontext issue and if I am even going about this the right way?
Thanks for the help.
Add Window:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class AddWindow : Window, INotifyPropertyChanged
{
private MainWindow mainW;
public AddWindow(MainWindow mW)
{
InitializeComponent();
mainW = mW;
this.Show();
DataContext = this;
}
private void Finish(object sender, RoutedEventArgs e)
{
mainW.Reminders.Add(new Remind(SelectedDate, Message));
this.Close();
}
private DateTime selectedDate = DateTime.Today;
public DateTime SelectedDate
{
get
{
return selectedDate;
}
set
{
if (value != selectedDate)
{
selectedDate = value;
RaisePropertyChange("SelectedDate");
}
}
}
private string message;
public string Message
{
get
{
return message;
}
set
{
if (message != value)
{
message = value;
RaisePropertyChange("Message");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChange(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
Add Xaml
<TextBox Name="Time" HorizontalAlignment="Left" Height="28" Margin="124,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115"/>
<DatePicker SelectedDate="{Binding SelectedDate}" HorizontalAlignment="Left" Height="28" Margin="124,27,0,0" VerticalAlignment="Top" Width="115"/>
<TextBox Text="{Binding Msg}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="144"/>
<Button Content="Finish" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="135,226,0,0" Click="Finish" />
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private ObservableCollection<Remind> reminders = new ObservableCollection<Remind>();
public ObservableCollection<Remind> Reminders
{
get
{
return reminders;
}
}
private void Add(object sender, RoutedEventArgs e)
{
AddWindow addWindow = new AddWindow(this);
}
}
Mainwindow Xaml:
</MenuItem>
<MenuItem Header="About">
<MenuItem Header="Info"/>
</MenuItem>
</Menu>
<Button Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" Click="Add" />
<Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" />
<Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" />
<ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346">
<ListBox ItemsSource= "{Binding Reminders}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="41" Width="293" >
<TextBlock Text="{Binding Path=dateT}"/>
<TextBlock Text="{Binding Path=Msg}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
<Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/>
Remind :
public class Remind : INotifyPropertyChanged
{
public Remind(DateTime dt, string ms)
{
DateT = dt;
Msg = ms;
}
private DateTime datet;
public DateTime DateT
{
get
{
return datet;
}
set
{
if (datet != value)
{
datet = value;
RaisePropertyChange("dateT");
}
}
}
private string msg;
public string Msg
{
get
{
return msg;
}
set
{
if (msg != value)
{
msg = value;
RaisePropertyChange("Msg");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChange(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
Change dateT to DateT in your main window
<TextBlock Text="{Binding Path=DateT}"/>
and you are done.
Under the bottom line everthing with the datacontext was ok. Your the 2 wrong property names were missspelled.
Hm, I created a small solution with your code and it just works fine. The main windows's list gets updated right after I click finish. The only small problem is you use the wrong binding in AddWindow to the message. You bind to "Msg" but it should be "Message" in the 3rd line above:
<TextBox Text="{Binding Message}" HorizontalAlignment="Left" Height="58" Margin="123,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="144"/>
Code looks fine but I can see one issue in it:
TextBox in AddWindow is binded with Msg but the corresponding property name in code behind is Message. So, textBox is never binded properly and hence new item is added in collection with String.Empty value for Msg.
<TextBox Text="{Binding Msg}" <-- HERE. It should be Message.
However, it should still show a new object in collection on GUI with empty string and DateTime value set on AddWindow even in case of binding failure.
For updated Remind class in question:
One issue in XAML binding where you are binding with field instead of it's wrapper property.
<TextBlock Text="{Binding Path=dateT}"/> <-- HERE, Path name should be DateT.

Hide list view until button is pressed

Im having text box and list view and when you are pressing on the button
you are the list view is filled with data ,currently the list view is under the button and the text box and
always is there and filled after you press on the button.
There is a way to hide the list view from the page until you press on the button and requesting the data?
public class ModelView
{
public ModelView()
{
GetServiceCollection = new ObservableCollection<string>();
}
bool isDataLoaded = false;
MyCommand goCommand;
public ICommand GoCommand
{
get { return goCommand ?? (goCommand = new MyCommand(() => OnGoCommand(), () => !isDataLoaded)); }
}
public ObservableCollection<string> GetServiceCollection { get; set; }
void OnGoCommand()
{
GetServiceCollection.Clear();
foreach (var item in _configServiceModel.CollectList)
{
GetServiceCollection.Add(item);
}
isDataLoaded = true;
goCommand.RaiseCanExecuteChanged();
}
......
The xaml
<Button Content="Go" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="75" Height="21.96" Command="{Binding GoCommand}"/>
<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230"
Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330"
ItemsSource="{Binding GetCollection}" }" >
}
ViewModel
public class ConfigModelView:INotifyPropertyChanged
{
public ConfigModelView()
{
GetServiceCollection=new ObservableCollection<string>();
}
bool isDataLoaded;
public bool IsDataLoaded
{
get { return isDataLoaded; }
set { isDataLoaded = value; OnPropertyChanged("IsDataLoaded"); }
}
MyCommand goCommand;
public ICommand GoCommand
{
get{return goCommand ?? (goCommand=new MyCommand(()=>Command(),()=>!isDataLoaded));}
}
public ObservableCollection<string> GetServiceCollection{get;set;}
void Command()
{
foreach (var item in _configServiceModel.CollectList)
{
GetServiceCollection.Add(item);
}
isDataLoaded = true;
OnPropertyChanged("IsDataLoaded");
goCommand.RaiseCanExecuteChanged();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
BooleanToVisibilityConverter
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
xaml
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
Title="Window1" Height="300" Width="800">
<Window.Resources>
<local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
<Button Content="Go" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="75" Height="21.96" Command="{Binding GoCommand}"/>
<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230"
Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330"
Visibility="{Binding IsDataLoaded,
Converter= {StaticResource BoolToVisibilityConverter}}"
ItemsSource="{Binding GetCollection}" />
</StackPanel>
The best bet here would be to create another property on the ViewModel that you bind the Visibility of the ListView to. In the GoCommand implementation, set this property to visible.
As a side note, your ViewModel doesn't implement INotifyPropertyChanged, so you'll need to do that as well to have the visiblity update when the property is changed:
private Visibility listViewVisibility;
public Visibility ListViewVisibility
{
get { return listViewVisibility; }
set
{
if (this.listViewVisibility == value)
return;
this.listViewVisibility = value;
this.OnPropertyChanged("ListViewVisibility");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if(this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
The xaml:
<ListView Grid.Column="2" HorizontalAlignment="Center" Height="230"
Margin="5,20,0,0" Grid.Row="2" VerticalAlignment="Top" Width="330"
Visibility="{Binding ListViewVisibility}"
ItemsSource="{Binding GetCollection}" />
Well load your form then in the put "listView1.hide()".
Then create your button event.
Type "listView1.show()".
P.S. you can also set all of those values in your c# code.

WPF Binding to Radio Buttons If Radio Buttons duplicated to same property .,.,. not updating the Model

Here This is the View which the Radio buttons had been bound to a Enum Property using IValueConverter(Works Fine)
if I duplicate these radio Buttons as Below XAML
<Grid.Resources>
<l:enumBoolConverter x:Key="ebc" />
<l:EnumStringConverter x:Key="es" />
</Grid.Resources>
<StackPanel>
<StackPanel>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Yes}">Yes</RadioButton>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=No}">No</RadioButton>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Maybe}">Maybe</RadioButton>
<Separator />
<TextBox Text="{Binding YesOrNo, Converter={StaticResource es}, Mode=OneWay}" />
</StackPanel>
<StackPanel>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Yes}">Yes</RadioButton>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=No}">No</RadioButton>
<RadioButton IsChecked="{Binding YesOrNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ebc}, ConverterParameter=Maybe}">Maybe</RadioButton>
<Separator />
<TextBox Text="{Binding YesOrNo, Converter={StaticResource es}, Mode=OneWay}" />
</StackPanel>
</StackPanel>
I can do Changing option From "Yes" to "No" but While am changing From "No" to "Yes" there is the problem which the property or view will not be updated..
Why..
I guess it,, for the second radio button of the "No" Option was once again called to Set the property "YesOrNo" while I am changing the First to "Yes"
is it so.. ??? What is the Resolution to Operate both the group... to one property.. and get updated on all the bindings.. ?
The CS Code for Value Convertions
[ValueConversion(typeof(YesOrNoOptions), typeof(bool))]
class enumBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var ps = parameter as string;
if (string.IsNullOrEmpty(ps))
return DependencyProperty.UnsetValue;
if (!Enum.IsDefined(typeof(YesOrNoOptions), value))
return DependencyProperty.UnsetValue;
var param = Enum.Parse(value.GetType(), ps);
var ret = param.Equals(value);
return ret;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var ps = parameter as string;
if (string.IsNullOrEmpty(ps))
{
return DependencyProperty.UnsetValue;
}
return Enum.Parse(targetType, ps);
}
}
[ValueConversion(typeof(YesOrNoOptions), typeof(string))]
internal class EnumStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Enum v = (Enum)value;
return v.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
CS Code of ViewModel
public enum YesOrNoOptions
{
Yes,
No,
Maybe
}
public class DemDC : INotifyPropertyChanged
{
public DemDC()
{
_yt = YesOrNoOptions.Yes;
}
public YesOrNoOptions _yt;
public YesOrNoOptions YesOrNo
{
get
{
return _yt;
}
set
{
_yt = value;
Notify("YesOrNo");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string pname)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(pname));
}
}
Thanks in Advance...
Any how the logic should be same for all the views isn't it?
While coming from "yes" to "No" both the panels are updated fine..
but while going back to "Yes" from "No" it stays there in the "No" option itself..
because the second radio button is setting the "No" value to enum... ??? while operating "Yes" isn't it???
if this is happening..? Why it is not happening while choosing from "Yes" to "No" ?????
To circumvent this issue you can disable property changes while the property is being updated:
private bool suppress;
private YesOrNoOptions _yt;
public YesOrNoOptions YesOrNo
{
get
{
return _yt;
}
set
{
if (_yt != value && !suppress)
{
_yt = value;
suppress = true;
Notify("YesOrNo");
suppress = false;
}
}
}

XAML TwoWay binding to Nullable types

Environment: WinRt / XAML / C#
I am trying to do a two way binding for a float type property. However if that is a nullable type (float? or Nullable) the binding doesnt work.
I have event applied converters for this, and it still doesnt show any sign of bindability.
C#: ViewModel
public class MyViewModel : INotifyPropertyChanged
{
private float _quantity1;
public float Quantity1
{
get
{
return this._quantity1;
}
set
{
this._quantity1 = value;
RaisePropertyChanged("Quantity1");
}
}
private float? _quantity2;
public float? Quantity2
{
get
{
return this._quantity2;
}
set
{
this._quantity2 = value;
RaisePropertyChanged("Quantity2");
}
}
private Nullable<float> _quantity3;
public Nullable<float> Quantity3
{
get
{
return this._quantity3;
}
set
{
this._quantity3 = value;
RaisePropertyChanged("Quantity3");
}
}
public MyViewModel()
{
this.Quantity1 = 100.01F;
this.Quantity2 = 200.02F;
this.Quantity3 = 300.03F;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
C# : Converter:
public sealed class NullableFloatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return 0F;
else
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (value != null)
return value;
else
return 0;
}
}
XAML:
<Page
x:Class="Test_Binding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Test_Binding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<local:NullableFloatConverter x:Key="nullConverter" />
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Quantity1: " Width="150" />
<TextBox Text="{Binding Quantity1, Mode=TwoWay}" />
<TextBlock Text="{Binding Quantity1}" />
</StackPanel>
<!-- the second text block doesnt get an updated value -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Quantity2: " Width="150"/>
<TextBox Text="{Binding Quantity2, Mode=TwoWay, Converter={StaticResource nullConverter}}" />
<TextBlock Text="{Binding Quantity2, Converter={StaticResource nullConverter}}" />
</StackPanel>
<!-- the second text block doesnt get an updated value -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Quantity2 (No Converter): " Width="150"/>
<TextBox Text="{Binding Quantity2, Mode=TwoWay}" />
<TextBlock Text="{Binding Quantity2}" />
</StackPanel>
<!-- the second text block doesnt get an updated value -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="Quantity3: " Width="150"/>
<TextBox Text="{Binding Quantity3, Mode=TwoWay}" />
<TextBlock Text="{Binding Quantity3}" />
</StackPanel>
</StackPanel>
</Grid>
Only the First text block gets updated (i.e. for Quantity1). I cant get the others (Quantity2 & Quantity3) to get updated.
Any suggestions ?
I think the convertBack method wasnt upto scratch, which i got from stackoverflow.com/questions/15406336/databind-a-nullable-type-in-xaml-windows-8-store-app
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
string s = value as string;
float result;
if (!string.IsNullOrWhiteSpace(s) && float.TryParse(s, out result))
{
return result;
}
return null;
}

Categories

Resources