Unable to hide the control in WPF using MVVM - wpf

When i selected the values in combo box,i have to hide another control. I have write the code as shown in below. Please correct me where i made mistake in that.
View Code:
<ComboBox x:Name="cboShowRuleWhere" Height="20" Width="200" ItemsSource="{Binding Source={StaticResource listedView}, Path=FilterRules}" DisplayMemberPath="RuleName" SelectedValuePath="RuleId" SelectedValue="{Binding Source={StaticResource listedView}, Path=SelectedRuleName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ></ComboBox>
<ComboBox Height="21" HorizontalAlignment="Left" Margin="6,4,0,0" x:Name="cboRuleCondtion" Visibility="{Binding Path=IsButtonVisible,Converter={StaticResource BoolToVisible}}" VerticalAlignment="Top" Width="212" />
ViewModel Code:
private DataTable m_selectedRuleName;
public DataTable SelectedRuleName
{
get
{
return m_selectedRuleName;
}
set
{
m_selectedRuleName = value;
base.RaisePropertyChangedEvent("SelectedRuleName");
}
}
private bool _IsButtonVisible;
public bool IsButtonVisible
{
get { return _IsButtonVisible; }
set
{
_IsButtonVisible = value;
base.RaisePropertyChangedEvent("IsButtonVisible");
}
}
Where i have to correct? Please help me asap. Thanks in advance..

There's not a whole lot to go on here. For instance, where are you setting IsButtonvisible based on your rule criteria? Here are some ideas:
1) Don't create a backing field for IsButtonVisible. Instead, have it return the correct analysis.
public bool IsButtonVisible { get { return SelectedRuleName == "IsVisibleRule"; } }
2) You can fire the Notify Propery Changed event from anywhere. In this case, you want the IsButtonVisible binding to be reevaluated every time the SelectedRuleName changes:
private DataTable m_selectedRuleName;
public DataTable SelectedRuleName
{
get
{
return m_selectedRuleName;
}
set
{
m_selectedRuleName = value;
base.RaisePropertyChangedEvent("SelectedRuleName");
base.RaisePropertyChangedEvent("IsButtonVisible");
}
}
3) Is the SelectedRuleName really a DataTable? That would seem odd to me because it indicates multiple rows. It would be a longer post, but I would avoid DataTable altogether and change the ComboBox item source to an ObservableCollection. The "SelectedRuleName" would be of type T (not DataTable).
4) Along the same lines, I have found much greater success using SelectedItem instead of SelectedValue.
I hope some of this helps.

Related

Set selected value of combobox

I want to set selected value of a combobox
I am receiving a DataTable from the database looking like this:
this Datatable is bound to this combobox.
<ComboBox
DisplayMemberPath="KommuneNavn"
SelectedValuePath="KommuneNr"
ItemsSource="{Binding KommuneNavne}"
SelectedValue="{Binding KommuneNr, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="3"
IsEnabled="{Binding IsUdenlandskAdresse, Converter={StaticResource BooleanNotConverter}}" />
In my viewmodel I have a specific KommuneNr stored in a property. I would like to have my combobox set to show the KommuneNavn that matches with this KommuneNr.
Example:
I have the KommuneNr 101 stored in my viewmodel, the KommuneNavn that matches with this is København I would then like to have my combobox be set to København.
This was pretty difficult to explain, I hope I am making sense. Otherwise feel free to ask.
KommuneNavne must be an ObservableCollection, and in your ViewModel you should implement INotifyPropertyChanged (not described in this example)
<ComboBox
ItemsSource="{Binding KommuneNavne}"
SelectedValue="{Binding KommuneNr, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="KommuneNavn"
SelectedValuePath="KommuneNr"
/>
ViewModel
public class MyViewModel : INotifyPropertyChanged
{
public ObservableCollection<KommuneNavn> KommuneNavne
{
get { value = _kommuneNavne; } //=> _kommuneNavne;
set
{
_kommuneNavne = value;
OnPropertyChanged(nameof(KommuneNavne));
}
}
public long KommuneNr
{
get { value = _kommuneNr; } //=> _kommuneNr;
set
{
if (_kommuneNr == value) return;
_kommuneNr = value;
OnPropertyChanged(nameof(KommuneNr));
}
}
private void SetValue(int valueToSet)
{
KommuneNr = valueToSet;
}
}

ComboBox SelectedItem initial value not from ItemsSource

I cannot understand the behavior of the combobox.
I have an edit view to edit existing Order data. Here is my VM of some part of Order data:
public class OrderDataViewModel : ViewModelBase, IOrderDataViewModel
{
private readonly ICustomersListProviderService _customersListProviderService;
private readonly Order _order;
public OrderDataViewModel(Order order, ICustomersListProviderService customersListProviderService)
{
Assign.IfNotNull(ref _order, order);
Assign.IfNotNull(ref _customersListProviderService, customersListProviderService);
}
public DateTime CreationDate
{
get { return _order.CreationDate ?? (_order.CreationDate = DateTime.Now).Value; }
set
{
if (_order.CreationDate == value) return;
_order.CreationDate = value;
OnPropertyChanged();
}
}
public Customer Customer
{
get { return _order.Customer; }
set
{
if (_order.Customer == value) return;
_order.Customer = value;
OnPropertyChanged();
}
}
private IList<Customer> _customersList;
public IList<Customer> CustomersList
{
get { return _customersList ?? (_customersList = _customersListProviderService.GetAllCustomers().ToList()); }
}
}
And XAML binding:
<ComboBox Grid.Row="2" Grid.Column="1"
SelectedItem="{Binding OrderDataViewModel.Customer}"
DisplayMemberPath="Name"
ItemsSource="{Binding OrderDataViewModel.CustomersList}"
/>
Description. Order comes from the database by the Repository, _customersListProviderService gets all customers from database also. I know that maybe it could be done better, but it's not the point of the question.
And... the issue is. After loading a window, my combobox has items collection filled (dropdown list is not empty) but the value is not set (its blank). Checking SelectedItem by code-behind results with null. I read a lot and found out, that you cannot set SelectedItem of combobox to the item that is not in ItemsSource.
Ok, my workaround was to change the Customer getter to:
public Customer Customer
{
get
{ return CustomersList.Single(c => c.Id == _order.Customer.Id); }
set
{
if (_order.Customer == value) return;
_order.Customer = value;
OnPropertyChanged();
}
}
now it works, but it does not look good to me.
Is there any better solution?
you can override Equals() and GetHashCode() in your entities and return Id.Equals() and Id.GetHashCode() respectively
I just had a similar issue within an UWP app. I was binding to a string array.
<ComboBox SelectedItem="{Binding Carrier, Mode=TwoWay}" ItemsSource="{Binding Carriers}" />
The problem solved when I changed ItemsSource to be before SelectedItem:
<ComboBox ItemsSource="{Binding Carriers}" SelectedItem="{Binding Carrier, Mode=TwoWay}" />
Just in case someone has a similar issue.
Perhaps adding SelectedValuePath="Name" to your xaml like below will help
<ComboBox Grid.Row="2" Grid.Column="1"
SelectedItem="{Binding OrderDataViewModel.Customer}"
DisplayMemberPath="Name"
ItemsSource="{Binding OrderDataViewModel.CustomersList}"
SelectedValuePath="Name"
/>
Create a ViewModel with both sets of data you need i.e set to fill ComboBox and the Record. I will use Customer and year for convenience.
class CustomerDetailsViewModel
{
public CustomertModel CutomerModel { get; set; }
public YearListModel YearList { get; set; }
public CustomerDetailsViewModel(CustomerModel _CustomerModel)
{
CustomerModel = _CustomerModel;
YearList = new YearListModel();
}
}
So I fill the combobox with a list of years and I have my selected Customer record.
<ComboBox x:Name="cbCustomerDetailsYear" Margin="128,503,0,0"
DataContext="{Binding DataContext,
ElementName=CustomerDetailsPage}"
ItemsSource="{Binding YearList.Years, Mode=OneWay}"
DisplayMemberPath="Description"
SelectedValue="{Binding CustomerModel.YearID}"
SelectedValuePath="id" />
The viewmodel is assigned to the Page Datacontext and I bind this to the Combobox.
The combobox is populated with Itemsource and Displaymember with the Year model list from my Viewmodel. The description is just a string saying 1999, 2000 or whatever
The SelectedValue is bound to the Year foreign key in the Customer record also in the ViewModel. The SelectedValuePath is the magic ingredient that binds the 2 together. So the id represents the id of the year but will be bound to the customer YearID field and set by the Combobox id value.
Hope this is clear ?!?!?!
I hope this would work:
Check if Customer, is already binded to another Control. If so, remove all other Bindings which involve Customer.
I had the same problem where the IsChecked property of a CheckBox was also Binded to the viewModel but using a converter to check if it's null or not. I changed its binding to a boolean and the problem was no more.

Binding Combobox column's selectedItem property Silverlight MVVM

I am using below Datagrid, (using MVVM pattern), here what I want is when I select something in the combobox, some kind of notification should happen in the ViewModel saying that this Row’s combobox selectedItem is changed to this value. Right now the notification is happening in the Set method of SelectedEname which is inside class SortedDetails(custom entity) and not a part of viewmodel. Please have a look at the code below and let me know If we can send the notification to videmodel in any way using MVVM pattern.
<c1:C1DataGrid x:Name="datagrid1" ItemsSource="{Binding Path=SortedDetailsList,Mode=TwoWay}" AutoGenerateColumns="False">
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay}"/>
<c1:DataGridTemplateColumn Header="ENGAGEMENT">
<c1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cmbEngagement" ItemsSource="{Binding EDetails, Mode=TwoWay}" DisplayMemberPath="EName" SelectedItem="{Binding SelectedEName,Mode=TwoWay}">
</ComboBox>
</DataTemplate>
</c1:DataGridTemplateColumn.CellTemplate>
</c1:DataGridTemplateColumn>
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
SortedDetailsList is a list of SortedDetails entity, which looks like this :-
public class SortedDetails
{
Private string name;
Private ObservableCollection<details> eDetails;
Private details selectedEname;
public string Name
{
get { return name; }
set { name = value; }
}
public ObservableCollection<details> EDetails
{
get { return eDetails; }
set { eDetails = value; }
}
public details SelectedEname
{
get { return selectedEname; }
set { selectedEname = value; }
}
}
public class Details
{
Private string eName;
Private int eId;
public string EName
{
get { return eName; }
set { eName = value; }
}
public int EId
{
get { return eId; }
set { eId = value; }
}
}
The reason i was asking this question was because i was looking to avoid writing code in codebehind, but in this case not able to avoid the same. So, here is the solution (for me) :-
Add an event delegate or any mediator pattern which will inform the ViewModel that selection is changed from the SelectionChanged event of Combobox...
You can put your ViewModel in the View's resources and bind to the property of ViewModel:
<ComboBox x:Name="cmbEngagement" ItemsSource="{Binding EDetails, Mode=TwoWay}" DisplayMemberPath="EName" SelectedItem="{Binding SelectedEName, Mode=TwoWay, Source={StaticResource ViewModel}">
where SelectedEName is a property of your ViewModel.
You want to use some mechanism for allowing events to invoke commands or verbs (methods) on your view model.
For example, using Actions in Caliburn.Micro, you could write the following:
<ComboBox x:Name="cmbEngagement" ...
cal:Message.Attach="[Event SelectionChanged] = [Action EngagementChanged($dataContext)]>
and in your view model:
public void EngagementChanged(SortedDetails details)
{
// access details.Name here
}
Note that actions in Caliburn.Micro bubble, so it would first look for an EngagementChanged method on SortedDetails type, and then look on your view model.

How can I add items from a listbox to a list by clicking a button without any codebehind?

I am new to MVVM, and also fairly new to WPF. As a matter of fact I started programming just a few months ago. MVVM is really dng my head in with the binding concept, and I have been trying for days now to just simply make an application that allows you to select an item from a listbx, and when you click on the add button the selected item should be saved in a new list. The second listbox displays the latest items added, and you can select an item and delete it by using another button. ususally I would go for the click event and decorate my codebehind with pretty little methods, but I really want to learn how to do all this by using bindings and no codebehind.
I would be extremly happy for any help, and please remember that I am new to this and I really want to keep it as simple as possible :)
with kind regards Daniela
<WrapPanel HorizontalAlignment="Center" Margin=" 10">
<ListBox x:Name="Firstbox"
Width="100"
ItemsSource="{Binding FoodList}"
DisplayMemberPath="Name" >
</ListBox>
<Button Margin="10 >Select</Button>
<ListBox Width="100"></ListBox>
private List _foodList;
public List<FoodItem> FoodList
{
get { return _foodList; }
set { _foodList = value; }
}
private List<FoodItem> _newFoodList;
public List<FoodItem> NewFoodList
{
get { return _newFoodList; }
set { _newFoodList = value; }
}
public MainViewModel()
{
InitializeCommands();
GetFood();
}
private void GetFood()
{
FoodList = new List<FoodItem>()
{
new FoodItem() {Name="Applepie"},
new FoodItem() {Name="Scones"}
};
}
first, you need to replace the Lists with ObservableCollections, so that the UI can detect when new items are added.
Add a SelectedItem property to your ViewModel:
private FoodItem _selectedItem;
public FoodItem SelectedItem
{
get { return _selectedItem;}
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
bind the SelectedItem property of the 1st ListBox to this property:
<ListBox Width=" 100" x:Name="Firstbox"
ItemsSource="{Binding FoodList}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedItem}" />
bind your 2nd ListBox to the NewFoodList property
create a command in your ViewModel:
private DelegateCommand _addItemCommand;
public ICommand AddItemCommand
{
get
{
if (_addItemCommand == null)
{
_addItemCommand = new DelegateCommand(AddItem);
}
return _addItemCommand;
}
}
void AddItem()
{
if (SelectedItem != null)
NewFoodList.Add(SelectedItem);
}
And finally, bind the button's Command property to the AddItemCommand property:
<Button Margin="10" Command="{Binding AddItemCommand}" >Select</Button>

Siliverlight databound combobox doesn't display initialized value

I am databinding a view to a viewmodel and am having trouble initializing a combobox to a default value. A simplification of the class I'm using in the binding is
public class LanguageDetails
{
public string Code { get; set; }
public string Name { get; set; }
public string EnglishName { get; set; }
public string DisplayName
{
get
{
if (this.Name == this.EnglishName)
{
return this.Name;
}
return String.Format("{0} ({1})", this.Name, this.EnglishName);
}
}
}
The combobox is declared in the view's XAML as
<ComboBox x:Name="LanguageSelector" Grid.Row="0" Grid.Column="1"
SelectedItem="{Binding SelectedLanguage,Mode=TwoWay}"
ItemsSource="{Binding AvailableLanguages}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and the viewmodel contains this code
private List<LanguageDetails> _availableLanguages;
private LanguageDetails _selectedLanguage;
public LoginViewModel()
{
_availableLanguages = LanguageManager.GetLanguageDetailsForSet(BaseApp.AppLanguageSetID);
_selectedLanguage = _availableLanguages.SingleOrDefault(l => l.Code == "en");
}
public LanguageDetails SelectedLanguage
{
get { return _selectedLanguage; }
set
{
_selectedLanguage = value;
OnPropertyChanged("SelectedLanguage");
}
}
public List<LanguageDetails> AvailableLanguages
{
get { return _availableLanguages; }
set
{
_availableLanguages = value;
OnPropertyChanged("AvailableLanguages");
}
}
At the end of the constructor both _availableLanguages and _selectedLanguage variables are set as expected, the combobox's pulldown list contains all items in _availableLanguages but the selected value is not displayed in the combobox. Selecting an item from the pulldown correctly displays it and sets the SelectedLanguage property in the viewmodel. A breakpoint in the setter reveals that _selectedLanguage still contains what it was initialized to until it is overwritten with value.
I suspect that there is some little thing I'm missing, but after trying various things and much googling I'm still stumped. I could achieve the desired result in other ways but really want to get a handle on the proper use of databinding.
You need to change the order of you bindings in XAML so that your ItemsSource binds before the SelectedItem.
<ComboBox x:Name="LanguageSelector" Width="100"
ItemsSource="{Binding AvailableLanguages}"
SelectedItem="{Binding SelectedLanguage,Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If you set a breakpoint on the 'get' of both the SeletedLanguage and AvailibleLanguage, you will notice that the SelectedLanguage gets hit before your AvailibleLanguage. Since that's happening, it's unable to set the SelectedLanguage because the ItemsSource is not yet populated. Changing the order of the bindings in your XAML will make the AvailibleLanguages get hit first, then the SelectedLanguage. This should solve your problem.
1) When you assign the SelectedLanguage, use the public property SelectedLanguage instead of the private _selectedLanguage, so that the setter gets executed,
2) You need to move the assignment of the selectedlanguage to the moment that the view has been loaded. You can do it by implementing the Loaded event handler on the View. If you want to be "mvvm compliant" then you should use a Blend behavior that will map UI loaded event to a viewmodel command implementation in which you would set the selected language.

Resources