Raise Guard method changed? - wpf

Here is code of my ViewModel:
public BindableCollection<ICaller> Callers { get { return callService.Current.Callers; } }
public void TalkPrivate(ICaller caller)
{
callService.TalkPrivate(caller);
}
public bool CanTalkPrivate(ICaller caller)
{
return caller.OnHold || callService.IsConference;
}
The XAML:
<ItemsControl ItemsSource="{Binding Callers}" Grid.Row="0" Margin="10,22,10,10"
Visibility="{Binding Callers.Count, Converter={StaticResource CollectionSizeToVisibilityConverter}, ConverterParameter=false}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<controls:CircleContentControl Height="40" Width="40" Grid.Column="0">
<Image Source="{Binding Image}" Stretch="Uniform" />
</controls:CircleContentControl>
<TextBlock Text="{Binding Display}" Grid.Column="1" FontSize="24" VerticalAlignment="Center" Margin="5,0,0,0"/>
<StackPanel Grid.Column="2" Orientation="Horizontal">
<Button cal:Message.Attach="TalkPrivate($dataContext)" Style="{StaticResource CallActionButtonStyle}" ToolTip="Falar privado"
Height="50" Width="50">
<Rectangle Width="20" Height="20" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{DynamicResource appbar_phone}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
Call Service will change OnHold property of the Callers. However, the UI is not changed accordinly; that is, the buttons are not disabled/enabled. It seems CanTalkPrivate is not called after TalkPrivate method is called.
How can I force the button availability to refresh?
EDITED for more insights of my code:
CallerViewModel
public class CallerViewModel : PropertyChangedBase, ICaller
{
public CallerViewModel(string phoneNumber, string name, string image = null)
{
PhoneNumber = phoneNumber;
Name = name;
Display = name;
Image = image;
}
public CallerViewModel(string phoneNumber, Contact contact)
: this(phoneNumber, contact.Display, contact.Image) { }
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyOfPropertyChange();
}
}
private string display;
public string Display
{
get { return display; }
set
{
display = value;
NotifyOfPropertyChange();
}
}
private string phoneNumber;
public string PhoneNumber
{
get { return phoneNumber; }
set
{
phoneNumber = value;
NotifyOfPropertyChange();
}
}
private string image;
public string Image
{
get { return image; }
set
{
image = value;
NotifyOfPropertyChange();
}
}
private bool onHold;
public bool OnHold
{
get { return onHold; }
set
{
onHold = value;
NotifyOfPropertyChange();
}
}
public void AppendToDisplay(string value)
{
if (Display == Name)
Display = value;
else
Display += value;
}
public void ResetDisplay()
{
Display = Name;
}
}
}
CallService:
public class CallService : ICallService
{
private readonly ICallerSearch callerSearch;
public bool IsInCall { get { return Current != null; } }
public bool IsConference { get; private set; }
public ICurrentCall Current { get; private set; }
public string IncomingPhoneNumber { get; set; }
public CallService(ICallerSearch callerSearch)
{
this.callerSearch = callerSearch;
}
public ICurrentCall CreateCall(string number)
{
var caller = callerSearch.FindByNumber(number);
Current = new CurrentCall(caller);
return Current;
}
public ICurrentCall CreateCall(ICaller caller)
{
Current = new CurrentCall(caller);
return Current;
}
public void EndCall(ICaller caller = null)
{
if (caller == null)
EndAll();
else
{
Current.Callers.Remove(caller);
if (IsConference)
IsConference = false;
}
}
private void EndAll()
{
if (Current != null)
Current.Dispose();
Current = null;
}
public ICaller AddCaller(string number)
{
foreach (var caller in Current.Callers)
caller.OnHold = true;
var newCaller = callerSearch.FindByNumber(number);
Current.Add(newCaller);
return newCaller;
}
public void MergeCalls()
{
IsConference = true;
}
public void TalkPrivate(ICaller caller)
{
foreach (var item in Current.Callers)
item.OnHold = true;
caller.OnHold = false;
if (IsConference)
IsConference = false;
}
}
#nigel, ICaller is in fact a ViewModel already. But ICallService isn't.
EDIT2 - SOLUTION
#Nigel pointed in the right direction. What I did was: CallerViewModel listen to ICallService ConferenceStarted and ConferenceEnded and update the CanTalkPrivate:
public CallerViewModel(ICallService callService, string phoneNumber, string name, string image = null)
{
this.callService = callService;
callService.ConferenceStarted += ConferenceStarted;
PhoneNumber = phoneNumber;
Name = name;
Display = name;
Image = image;
}
private void ConferenceStarted(object sender, System.EventArgs e)
{
NotifyOfPropertyChange(() => CanTalkPrivate);
}
private bool onHold;
public bool OnHold
{
get { return onHold; }
set
{
onHold = value;
NotifyOfPropertyChange();
NotifyOfPropertyChange(()=>CanTalkPrivate);
}
}
public bool CanTalkPrivate
{
get
{
return OnHold || callService.IsConference;
}
}
However, I can't use the guard method, because it still doesn't work. But now I can bind IsEnabled to CanTalkPrivate:
<Button cal:Message.Attach="TalkPrivate($dataContext)" IsEnabled="{Binding CanTalkPrivate}" Style="{StaticResource CallActionButtonStyle}" ToolTip="Falar privado"
Height="50" Width="50">
<Rectangle Width="20" Height="20" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{DynamicResource appbar_phone}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
Didn't like much all of this, but it works. Thanks everyone!

As most people have suggested using a property for CanTalkPrivate is best as you can control when notifications about changes happen. Using Guard methods instead of properties only work well when the parameters are coming from the UI and Caliburn.Micro can monitor them for property changes.
The way I tend to approach a problem like this where you have this sort of view logic a dedicated view model like CallerViewModel and Callers be a collection of these.
public class CallerViewModel : PropertyChangedBase
{
private readonly ICaller caller;
private readonly ICallService callService;
public CallerViewModel(ICaller caller, ICallService callService)
{
this.caller = caller;
this.callService = callService;
}
public void TalkPrivate()
{
callService.TalkPrivate(caller);
}
public bool CanTalkPrivate
{
get
{
return caller.OnHold || callService.IsConference;
}
}
}
You will still need a method of telling this view model that CanTalkPrivate has changed. If ICaller and ICallService use property changed notifications this could work. Of you may need a method on CallerViewModel that you can call when you know things have changed. It's hard to answer that part since I don't know how those two properties are altered, this part is specific to your app.
Edit due to question been updated.
Having ICaller as CallerViewModel certainly makes this solution a lot easier. Adding NotifyOfPropertyChange(() => CanTalkPrviate); to OnHold solves half the problem of correctly updating the UI correctly.
A potential solution for updating the UI on IsConferenceChanging is to one of a few solutions.
Use the event aggregator to broadcast a "call changed message" that CallerViewModel listens for.
Use simple .NET events on ICallService for when the call changes that CallerViewModel subscribes to.
Add code to the methods that use MergeCalls, AddCaller etc to update the CallerViewModel as well.

Caliburn.Micro uses databinding to evaluate when a guard method has to be called.
Implement INotifyPropertyChanged on your ICaller instances and raise PropertyChanged for OnHold and IsConference properties (if you haven't already done that).
Change your method signatures for both TalkPrivate and CanTalkPrivate to have parameters for every value that may change:
public bool CanTalkPrivate(ICaller caller, bool onHold, bool isConference)
{
return onHold || isConference;
}
Change your Action Message accordingly:
TalkPrivate($datacontext, $datacontext.OnHold, $datacontext.IsConference)
Alternatively, you could create an additional property CanTalkPrivate and use that instead:
TalkPrivate($datacontext, $datacontext.CanTalkPrivate)

Or another option is to change it so that the CanTalkPrivate is a bool property and just make a Notification call when you have a change on the underlying service.
public bool CanTalkPrivate
{
get{ return OnHold || IsConference;}
}
NotifyOfPropertyChange(() => CanTalkPrivate);

Related

Value of binded property don´t change when enter text in textbox (INotifyPropertyChanged implemented, MVVM)

I am trying to realize Wordle(Game) in WPF as a project to practice. In the ViewModel I have a Property(e.g. FirstWord) for the first, second, third, fourth and fifth word, all of which have INotifyPropertyChanged implemented. I would like to avoid creating a property for every single textbox (would be 5x5). Therefore, my model consists of 1st letter, 2nd letter, 3rd letter, etc. In XAML the binding looks like this: {Binding FirstWord.FirstLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}.
When debugging, the program does not even jump into the setter, it was not detected that the value has changed. What can be the reason for this?
ViewModel
public class WordleViewModel : ObservableObject
{
private int counter;
public string solutionWord;
private static int[] _stateColor = new int[5];
public static int[] StateColor
{
get { return _stateColor;} private set { _stateColor = value; }
}
private SafeWordsModel _firstWord;
public SafeWordsModel FirstWord
{
get { return _firstWord; }
set
{
_firstWord = value;
OnPropertyChanged();
}
}
//....
public WordleViewModel()
{
solutionWord = "TESTE";
}
}
Model (Do I really need variable + Property and the INotifyChanged here?)
public class SafeWordsModel : ObservableObject
{
private string _firstLetter;
private string _secondLetter;
private string _thirdLetter;
private string _fourthLetter;
private string _fifthLetter;
public string FirstLetter
{
get { return _firstLetter; }
set { _firstLetter = value; OnPropertyChanged(); }
}
public string SecondLetter
{
get { return _secondLetter; }
set { _secondLetter = value; OnPropertyChanged(); }
}
public string ThirdLetter
{
get { return _thirdLetter; }
set { _thirdLetter = value; OnPropertyChanged(); }
}
public string FourthLetter
{
get { return _fourthLetter; }
set { _fourthLetter = value; OnPropertyChanged(); }
}
public string FifthLetter
{
get { return _fifthLetter; }
set { _fifthLetter = value; OnPropertyChanged(); }
}
public SafeWordsModel()
{
this.FirstLetter = _firstLetter;
this.SecondLetter = _secondLetter;
this.ThirdLetter = _thirdLetter;
this.FourthLetter = _fourthLetter;
this.FifthLetter = _fifthLetter;
}
}
View
public WordleView()
{
InitializeComponent();
ViewModel = new WordleViewModel();
}
public WordleViewModel ViewModel
{
get { return DataContext as WordleViewModel; }
set { DataContext = value; }
}
public void FocusNext(object sender, TextChangedEventArgs e)
{
if (((TextBox)sender).MaxLength == ((TextBox)sender).Text.Length)
{
// move focus
var ue = e.OriginalSource as FrameworkElement;
e.Handled = true;
ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
XAML for one Word
<StackPanel Orientation="Horizontal"
FocusManager.FocusedElement="{Binding ElementName=tbx11}">
<TextBox x:Name="tbx11"
Style="{StaticResource Input_tbx}"
TextChanged="FocusNext"
Background="{Binding ElementName=Line2_ckb, Path=IsChecked, Converter={StaticResource brush1}}"
Text="{Binding FirstWord.FirstLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="tbx12"
Style="{StaticResource Input_tbx}"
TextChanged="FocusNext"
Background="{Binding ElementName=Line2_ckb, Path=IsChecked, Converter={StaticResource brush2}}"
Text="{Binding FirstWord.SecondLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="tbx13"
Style="{StaticResource Input_tbx}"
TextChanged="FocusNext"
Text="{Binding FirstWord.ThirdLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="tbx14"
Style="{StaticResource Input_tbx}"
TextChanged="FocusNext"
Background="{Binding ElementName=Line2_ckb, Path=IsChecked, Converter={StaticResource brush4}}"
Text="{Binding FirstWord.FourthLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Name="tbx15"
TextChanged="FocusNext"
Style="{StaticResource Input_tbx}"
Background="{Binding ElementName=Line2_ckb, Path=IsChecked, Converter={StaticResource brush5}}"
Text="{Binding FirstWord.FifthLetter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
ObservableObject Class
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
So far it has always worked well with INotifyPropertyChanged. It should set the value that comes from the textbox accordingly in the property and I can also use this value. Feel free to make suggestions on other places in the code, I'm still very new to WPF.

WPF - How to change content of ContentControl clicking on TreeViewItem?

I am working on a WPF project and I am facing a problem. I have searched for 2 days a solution but I have never found anything that could help me...
I have a TreeView with different data types inside and a ContentControl that shows views corresponding to these different data types. I want, when I click on a TreeViewItem that, depending on the data type contained in this TreeViewItem, it changes the view in the ContentControl.
I reached to execute different commands depending on the TreeviewItem selected but it never changes the view...
Does somebody have an answer or an idea that could help me ?
I use this for my TreeView :
<TreeView x:Name="networkTree" ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="490" TreeViewItem.Selected="GetHeaderSelectedItem"/>
It executes this :
public class NetworkConfigViewModel : BindableBase
{
private IRegionManager regionManager;
private bool _showRDConf;
private bool _showNetworkConf;
private bool _showDeviceInfo;
public NetworkConfigViewModel(IRegionManager regionManager)
{
this.regionManager = regionManager;
regionManager.RegisterViewWithRegion("NetworkConfigInfoRegion", typeof(NetworkConfigInfoView));
regionManager.RegisterViewWithRegion("RDConfigurationRegion", typeof(RDConfigurationView));
regionManager.RegisterViewWithRegion("DeviceInfoRegion", typeof(DeviceInfoView));
ShowNetworkConfCommand = new DelegateCommand(NetworkConf);
ShowRDConfCommand = new DelegateCommand(RDConf);
ShowDeviceInfoCommand = new DelegateCommand(DevInfo);
_showNetworkConf = true;
_showRDConf = false;
_showDeviceInfo = false;
}
public bool ShowRDConf
{
get
{
return _showRDConf;
}
set
{
SetProperty(ref _showRDConf, value);
}
}
public bool ShowNetworkConf
{
get
{
return _showNetworkConf;
}
set
{
SetProperty(ref _showNetworkConf, value);
}
}
public bool ShowDeviceInfo
{
get
{
return _showDeviceInfo;
}
set
{
SetProperty(ref _showDeviceInfo, value);
}
}
public DelegateCommand ShowNetworkConfCommand { get; set; }
public DelegateCommand ShowRDConfCommand { get; set; }
public DelegateCommand ShowDeviceInfoCommand { get; set; }
private void NetworkConf()
{
ShowRDConf = false;
ShowDeviceInfo = false;
ShowNetworkConf = true;
System.Windows.Forms.MessageBox.Show("Commande ShowNetConf executée :\nShowNetworkConf="+ShowNetworkConf.ToString()+"\nShowRDConf="+ShowRDConf.ToString() + "\nShowDeviceInfo=" + ShowDeviceInfo.ToString());
}
private void RDConf()
{
ShowNetworkConf = false;
ShowDeviceInfo = false;
ShowRDConf = true;
System.Windows.Forms.MessageBox.Show("Commande ShowRConf executée :\nShowRDConf="+ShowRDConf.ToString()+"\nShowNetworkConf="+ShowNetworkConf.ToString() + "\nShowRDeviceInfo=" + ShowDeviceInfo.ToString());
}
private void DevInfo()
{
ShowNetworkConf = false;
ShowRDConf = false;
ShowDeviceInfo = true;
System.Windows.Forms.MessageBox.Show("Commande ShowDeviceInfo executée :\nShowDeviceInfo=" + ShowDeviceInfo.ToString() + "\nShowNetworkConf=" + ShowNetworkConf.ToString() + "\nShowRDConf=" + ShowRDConf.ToString());
}
public void ChangeNetworkView(TreeView tree)
{
TreeViewItem tvi = null;
tvi = tree.SelectedItem as TreeViewItem;
if (tvi != null)
{
if (tvi.Tag.ToString() == "Network")
{
ShowNetworkConfCommand.Execute();
}
else if(tvi.Tag.ToString()=="RD")
{
ShowRDConfCommand.Execute();
}
else if (tvi.Tag.ToString() == "VD")
{
ShowDeviceInfoCommand.Execute();
}
}
}
}
And for my ContentControl :
<ContentControl x:Name="RDView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowRDConf, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="RDConfigurationRegion"/>
<ContentControl x:Name="NetworkView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowNetworkConf, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="NetworkConfigInfoRegion"/>
<ContentControl x:Name="DeviceView" x:FieldModifier="public" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="{Binding Path=ShowDeviceInfo, Converter={StaticResource Convert}}" prism:RegionManager.RegionName="DeviceInfoRegionq"/>
Thank you by advance

wpf ListItem SelectedValue Object is always null

I have a simple ListBox and a TextBox as under.I want to display the selectedvalue property of Listbox in the textbox,but my ViewModel's selected object is always null.
What am i missing here?
My XAML
<StackPanel>
<Canvas>
<TextBox x:Name="TxtMail" Width="244" FontSize="14" Canvas.Left="36" Canvas.Top="34" Height="20" Text="{Binding CurrentRec.Name,Mode=OneWay}" />
<ListBox x:Name="AllMatching" Width="{Binding ElementName=TxtMail,Path=Width}" Height="100" Canvas.Top="54" Canvas.Left="36" DisplayMemberPath="Name" SelectedItem="{Binding CurrentRec,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedValue="Name" SelectedValuePath="Name" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" />
<Button Content="Test" x:Name="cmdtest" Click="cmdtest_Click"/>
</Canvas>
My ViewModel:
public class VM_Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int p_ID;
public double p_SP, p_CP;
public string p_Name;
public List<DM_Data> AllData;
public DM_Data CurrentRec;
public VM_Data()
{
LoadData();
}
public int ID
{
get { return p_ID; }
set
{
if (p_ID != value)
{
RaisePropertyChangedEvent("ID");
p_ID = value;
}
}
}
public double SP
{
get { return p_SP; }
set
{
if (p_SP != value)
{
RaisePropertyChangedEvent("SP");
p_SP = value;
}
}
}
public double CP
{
get { return p_CP; }
set
{
if (p_CP != value)
{
RaisePropertyChangedEvent("CP");
p_CP = value;
}
}
}
public string Name
{
get { return p_Name; }
set
{
if (p_Name != value)
{
RaisePropertyChangedEvent("Name");
p_Name = value;
}
}
}
private void LoadData()
{
AllData = new List<DM_Data>();
string[] strNames = "Jatinder;Shashvat;shashikala;shamsher;shahid;justin;jatin;jolly;ajay;ahan;vijay;suresh;namita;nisha;negar;zenith;zan;zen;zutshi;harish;hercules;harman;ramesh;shashank;mandeep;aman;amandeep;amarjit;asim;akshay;amol;ritesh;ritivik;riz;samana;samaira;bhagwandass;bhagwan;bhawna;bhavna".Split(';');
for(int i=0;i<=strNames.GetUpperBound(0);i++)
{
DM_Data NewRec = new DM_Data();
NewRec.CP = new Random().Next(200, 400);
NewRec.SP = new Random().Next(1, 10);
NewRec.ID = i + 1;
NewRec.Name = strNames[i];
AllData.Add(NewRec);
}
AllData = AllData.OrderBy(item => item.Name).ToList();
}
private void RaisePropertyChangedEvent(string Property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Property));
}
}
}
My DataModel
public class DM_Data
{
public int p_ID;
public double p_SP, p_CP;
public string p_Name;
public int ID
{
get { return p_ID; }
set { p_ID = value; }
}
public double SP
{
get { return p_SP; }
set { p_SP = value; }
}
public double CP
{
get { return p_CP; }
set { p_CP = value; }
}
public string Name
{
get { return p_Name; }
set { p_Name = value; }
}
MainWindow.Xaml.cs
public partial class MainWindow : Window
{
VM_Data ViewModel;
public MainWindow()
{
InitializeComponent();
ViewModel = new VM_Data();
this.DataContext = ViewModel;
AllMatching.ItemsSource = ViewModel.AllData;
}
private void cmdtest_Click(object sender, RoutedEventArgs e)
{
DM_Data crec = ViewModel.CurrentRec;
}
}
CurrentRec must be a property that raises the PropertyChanged event:
private DM_Data _currentRec;
public DM_Data CurrentRec
{
get { return _currentRec; }
set { _currentRec = value; RaisePropertyChangedEvent("CurrentRec"); }
}
In the code you have posted, it is a field and you cannot bind to fields:
public DM_Data CurrentRec;
You can't bind to fields! CurrentRec must be a property. At now it is a field.
Why do you set ItemsSource in code-behind? Set it in XAML.
You should call RaisePropertyChangedEvent after you've changed backing field, not before.
It is not right pattern for events raising: if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(Property)); } You need to save event delegate to variable first or use ?.Invoke.
Don't create new instances of Random on every iteration of loop because you will get equal values. Create the only one outside of the loop and use it.
What you are doing is kind of MVVM, but not really.
Here is a quick fix anyway:
Please take a look at the Bindings.
<StackPanel>
<Canvas>
<TextBox x:Name="TxtMail" Width="244" FontSize="14" Canvas.Left="36" Canvas.Top="34" Height="20" Text="{Binding ElementName=AllMatching, Path=SelectedItem.Name}" />
<ListBox x:Name="AllMatching"
Width="{Binding ElementName=TxtMail,Path=Width}"
Height="100"
Canvas.Top="54"
Canvas.Left="36"
DisplayMemberPath="Name"
SelectedItem="{Binding CurrentRec,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto" />
<Button Content="Test" x:Name="cmdtest" Click="cmdtest_Click"/>
</Canvas>
</StackPanel>
I think you get the idea at: Text="{Binding ElementName=AllMatching, Path=SelectedItem.Name}".
Aditional Information
First:
You fire to early dude. Please first assign the value and then say its changed.
if (p_Name != value)
{
RaisePropertyChangedEvent("Name");
p_Name = value;
}
Second:
Use a ObservableCollection<DM_Data> to let your ListBox know about changes.
Third:
Use the posibility of Binding
Remove AllMatching.ItemsSource = ViewModel.AllData; and go like
<ListBox x:Name="AllMatching"
ItemsSource="{Binding Path=AllData}"
...
/>
And after all of this - please man check out some tutorials. And also refactor your code from VM_Data to DataViewModel thank you sir.

How to make the CanExecute trigger properly using Prism 6

I have a Model
public class Irritant : BindableBase
{
private short _id;
private string _name;
private string _description;
public short Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
public string Description
{
get { return _description; }
set { SetProperty(ref _description, value); }
}
public Irritant()
{
Id = 0;
Name = "";
Description = "";
}
}
Then my ViewModel with two versions
public class IrritantViewModel : BindableBase
{
private IrritantDb db = new IrritantDb();
//Version 1 - The Model's property is coded in IrritantViewModel
//private short _id;
//private string _name = "Alen";
//private string _description;
//public short Id
//{
// get { return _id; }
// set { SetProperty(ref _id, value); }
//}
//public string Name
//{
// get { return _name; }
// set { SetProperty(ref _name, value); }
//}
//public string Description
//{
// get { return _description; }
// set { SetProperty(ref _description, value); }
//}
//Version2 - I use the Irritant Model as property of IrritantViewModel
private DateTime? _lastUpdated;
private Irritant _entity;
public Irritant Entity
{
get { return _entity; }
set { SetProperty(ref _entity, value); }
}
public DateTime? LastUpdated
{
get { return _lastUpdated; }
set { SetProperty(ref _lastUpdated, value); }
}
public DelegateCommand UpdateCommand { get; set; }
public IrritantViewModel()
{
Entity = new Irritant();
//Version1
//UpdateCommand = new DelegateCommand(EditCommand, CanExecute).ObservesProperty(() => Name);
//Version2
UpdateCommand = new DelegateCommand(EditCommand, CanExecute).ObservesProperty(() => Entity.Name);
}
private bool CanExecute()
{
//Version1
//switch (Name)
//{
// case null:
// return false;
// case "":
// return false;
//}
//Version2
switch (Entity.Name)
{
case null:
return false;
case "":
return false;
}
return true;
}
private void EditCommand()
{
LastUpdated = DateTime.UtcNow;
}
}
And this is my View
public partial class IrritantView : UserControl
{
public IrritantView()
{
InitializeComponent();
DataContext = new IrritantViewModel();
}
}
<Grid >
<ScrollViewer>
<StackPanel MinWidth="200">
<TextBlock Text="Irritant" />
<!--Version 1-->
<!--<TextBlock Text="Name" />
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Description" />
<TextBox Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
-->
<!--Version 2-->
<TextBlock Text="Name" />
<TextBox Text="{Binding Entity.Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Description" />
<TextBox Text="{Binding Entity.Description, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Last Updated" />
<Label Content="{Binding LastUpdated, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Save"
Command="{Binding UpdateCommand}"
/>
</StackPanel>
</ScrollViewer>
</Grid>
The Version1 works fine, the Save Button disables when the TextBox that is bound to Name (TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}") is null or empty.
But with version 2, the Save Button doesn't disable. It only calls the method CanExecute during initialization, removing the text in the TextBox doesn't disable the Button. What did I do wrong?
DelegateCommand doesn't raise CanExecuteChanged event automatically, you have to raise that event manually by calling RaiseCanExecuteChanged when appropriate. Apart from using DelegateCommand, you can use RelayCommand which relays on CommandManager.RequerySuggested event which do the similar thing for you.
Change your Command definition returning ICommand:
public ICommand UpdateCommand { get; set; }
Initialize the command using below:
UpdateCommand = new AutoCanExecuteCommand(new DelegateCommand(EditCommand, CanExecute));
Use the following class as a wrapper:
public class AutoCanExecuteCommand : ICommand
{
public ICommand WrappedCommand { get; private set; }
public AutoCanExecuteCommand(ICommand wrappedCommand)
{
if (wrappedCommand == null)
{
throw new ArgumentNullException("wrappedCommand");
}
WrappedCommand = wrappedCommand;
}
public void Execute(object parameter)
{
WrappedCommand.Execute(parameter);
}
public bool CanExecute(object parameter)
{
return WrappedCommand.CanExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
I wouldn't recommend hooking into the CommandManager for a number of reasons. Besides memory leaks, it can introduce performance problems in your apps since you have no control over when, or how many times, the CommandManager will invoke the CanExecute (which happens on the UI thread). Instead, I would recommend using the INPC of your model object instead as shown in this answer:
ObservesProperty method isn't observing model's properties at Prism 6

Wpf binding is not working properly

My ViewModel
public class MyViewModel : ViewModelBase
{
// INotifyPropertyChanged is implemented in ViewModelBase
private String _propX;
public String PropX
{
get { return _propX; }
set
{
if (_propX != value)
{
_propX = value;
RaisePropertyChanged(() => PropX);
}
}
}
private String _propY;
public String ServerIP
{
get { return _propY; }
set
{
if (_propY != value)
{
_propY = value;
RaisePropertyChanged(() => ServerIP);
}
}
}
public A()
{
this._propY = "000.000.000.000";
this._propY = "000.000.000.000";
}
}
// EDIT
// This is the command that resets the properties
private RelayCommand _resetFormCommand;
public ICommand ResetConnectionFormCommand
{
get
{
if (_resetFormCommand == null)
{
_resetFormCommand = new RelayCommand(param => this.ExecuteResetFormCommand(), param => this.CanExecuteResetFormCommand);
}
return _resetFormCommand;
}
}
private bool CanExecuteResetFormCommand
{
get
{
return !String.IsNullOrWhiteSpace(this._propX) ||
!String.IsNullOrWhiteSpace(this._propY);
}
}
private void ExecuteResetFormCommand()
{
this._propX = "";
this._propY = "";
}
My View xaml
<TextBox Name="propX" Text="{Binding PropX }" PreviewTextInput="textBox_PreviewTextInput" />
<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput" />
<Border>
<Button Content="Reset" Name="resetBtn" Command="{Binding ResetFormCommand}" />
</Border>
My View code behind
private MyViewModel vm;
public ConnectionUserControl()
{
InitializeComponent();
vm = new MyViewModel();
this.DataContext = vm;
}
private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
ValidateInput(sender as TextBox, e);
}
The reset command resets the properties in my view model but the textboxes still contain their values, the binding is not working properly :(
Am i missing something here?
You should reset the properties, not the private members:
private void ExecuteResetFormCommand()
{
this.PropX = "";
this.PropY = "";
}
How are you resetting the values? You may be overriding the databinding when you reset the values. It would be helpful if you post the code that gets executed when the button is clicked.
In your xaml-code you have to set the binding like:
<TextBox Name="propX" Text="{Binding PropX, Mode=TwoWay}" .../>
binding has to be two way in order for textbox to update itself from viewmodel
In your Code-behind, you have a property ServerIP, which I think you wanted to be named as PropY, since your TextBox binds to a PropY property.
<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput" /
Also, you should be assigning the value to your property in your ExecuteResetFormCommand command, and not your private member since the private member does not trigger INotifyPropertyChanged
private void ExecuteResetFormCommand()
{
this.PropX = "";
this.PropY = ""; // <-- Currently you have PropY declared as ServerIP
}

Resources