I have 2 combobox master-slave in this way:
<ComboBox ItemSource="{Binding MySource}" SelectedItem="{Binding MySelectedItem}" DisplayMemberPath="Description" />
<ComboBox ItemSource="{Binding MySelectedItem.Items}" IsSynchronizedWithCurrentItem="{x:Null}" />
But when I select one item of first combobox which it has empty list of Items after I have selected one of them with Items and selected item in second combobox. The text in second combobox is not empty. I have tried with IsSynchronizedWithCurrentItem="False" too.
What is the problem?
I am not sur to understand what you are trying to say... But I am pretty sur that you didn't notify a PropertyChanged after modify your "MySelectedItem" and you forget the mode=TwoWay...
If you want to use the SelectedItem in your ViewModel:
Xaml:
<ComboBox ItemSource="{Binding MySource}" SelectedItem="{Binding MySelectedItem, mode=TwoWay}" DisplayMemberPath="Description" />
<ComboBox ItemSource="{Binding MySelectedItem.Items}"/>
ViewModel:
private YourItemType _mySelectedItem;
public YourItemType MySelectedItem
{
get { return (_mySelectedItem);}
set
{
if (_mySelectedItem != value)
{
_mySelectedItem = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("MySelectedItem"));
}
}
}
If you just want to do a filtering:
<ComboBox ItemSource="{Binding MySource}" DisplayMemberPath="Description" name="source"/>
<ComboBox ItemSource="{Binding SelectedItem.Items, ElementName=source}"/>
I have found the problem, it was that I have associated a command with interactivity in the combobox and this was catching an exception and didn't continue with the execution.
Related
My problem is that after I select a item in the ComboBox, the first item or "default" item of the combobox stays empty but if I click the combobox the values beneath show up are selectable etc. but I want the clicked one to show in the "default/first" place.
What I tried so far
XAML:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding TabItems, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
DisplayMemberPath="Header">
</ComboBox>
Property:
public TabItem SelectedItem {
get {
return _selectedItem;
}
set {
UpdateTCVCollection(value);
_selectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
}
If I open up the combobox the selecteditem is highlighted, but I also want it to be shown in the "first place" when the ComboBox is closed.
You can add a method when the index has changed, then remove the item that the user selected and add it at the begining.
I've set the value of Sorted to false because that way the value you selected won't be reorganized in your ComboBox.
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) {
RadItem selectedItem = ComboBox1.SelectedItem as RadItem;
if (selectedItem != null) {
ComboBox1.Items.Remove(selectedItem);
ComboBox1.Items.Sorted = true;
ComboBox1.Items.Sorted = false;
ComboBox1.Items.Insert(0, selectedItem);
ComboBox1.Text = selectedItem.Text;
}
}
Add the UpdateSourceTrigger to your Combobox.
UpdateSourceTrigger=PropertyChanged
Example:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding TabItems, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Header">
</ComboBox>
Have a look to this MSDN link
That should help you with your problem.
Greetings
I need the ComboBox below to keep the SelectedItem when I edit the text, at the moment as soon as I edit the text the SelectedItem turns null.
<ComboBox x:Name="FilterGroups"
IsEditable="True"
DisplayMemberPath="Code"
Text="{Binding FilterGroupCode}"
ItemsSource="{Binding FilterGroups}"
SelectedItem="{Binding SelectedFilterGroup}"
Margin="10,0"/>
My goal is to detect that the item has been edited, currently I have no idea which item was edited since the SelectedItem becomes null.
And a null SelectedItem to me at the moment means "New" item.
As AGH's Comment.
Xaml
<ComboBox x:Name="myFilterGroups"
ItemsSource="{Binding FilterGroups}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedFilterGroup}"
IsEditable="True" Height="20"
LostFocus="myFilterGroups_LostFocus"
>
</ComboBox>
Code Behind
private void myFilterGroups_LostFocus(object sender, RoutedEventArgs e)
{
var selItem = myFilterGroups.SelectedItem;
if (selItem == null)
// Create New Item
;
}
i've two listbox binded with properties of my viewmodel, the first listbox shows LstEmpresas and work fine, when I select a item, the property SelectedCompany sets fine, all ok.
In SelectedCompany's set of my viewmodel, I call a method than pupulate a secondary list (LtsEjercicios) and work fine too (LtsEjercicios populate perfectly depends that item i've selected in the first listbox).
The secondary listbox binds his ItemSource from LtsEjercicios object that, across viewmodel, is updated.
But the secondary listbox NOT SHOW any data, i'm crazy yet.
This viewModel code
public class frmEmpresasVM : ViewModelBase
{
//propiedades
EmpresasDataAccess empresasController = new EmpresasDataAccess();
private ObservableCollection<EmpresasTable> lstEmpresas;
private ObservableCollection<EmpresasEjerciciosTable> ltsEjercicios;
public ObservableCollection<EmpresasTable> LstEmpresas
{
get
{
return lstEmpresas;
}
set
{
lstEmpresas = value; RaisePropertyChanged("LstEmpresas");
}
}
public ObservableCollection<EmpresasEjerciciosTable> LtsEjercicios
{
get
{
return ltsEjercicios;
}
set
{
ltsEjercicios = value; RaisePropertyChanged("LtsEjercicios");
}
}
//selected company in listbox
private int selectedCompany;
public int SelectedCompany
{
get
{
return selectedCompany;
}
set
{
selectedCompany = value;
LtsEjercicios = empresasController.SelectExercicesById(selectedCompany.ToString());
RaisePropertyChanged("SelectedCompany");
}
}
//main constructor, default values for lists
public frmEmpresasVM()
{
LstEmpresas = empresasController.SelectOnlyNames();
LtsEjercicios = empresasController.SelectExercicesById("0");
}
and, XAML for the view
<ListBox x:Name="companyList" HorizontalAlignment="Left" Height="205" Margin="20,30,0,0" VerticalAlignment="Top" Width="450" ItemsSource="{Binding LstEmpresas, Mode=OneWay}" SelectedValue="{Binding SelectedCompany, Mode=TwoWay}" SelectedItem="{Binding LtsEjercicios, Mode=OneWay}" SelectedIndex="0" DisplayMemberPath="Nombre" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True" SelectionChanged="companyList_SelectionChanged_1">
<ListBox.ItemBindingGroup>
<BindingGroup/>
</ListBox.ItemBindingGroup>
<ListBox.DataContext>
<InnovaCommerceHosteleryViewModels:frmEmpresasVM/>
</ListBox.DataContext>
</ListBox>
<TextBlock x:Name="lblEjercicio" HorizontalAlignment="Left" Height="20" Margin="475,10,0,0" TextWrapping="Wrap" Text="Ejercicios" VerticalAlignment="Top" Width="95"/>
<ListBox x:Name="excercicesList" HorizontalAlignment="Left" Height="205" Margin="475,30,0,0" VerticalAlignment="Top" Width="110" ItemsSource="{Binding LtsEjercicios, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Ejercicio" SelectedValuePath="Id" SelectedIndex="0" IsSynchronizedWithCurrentItem="True">
<ListBox.DataContext>
<InnovaCommerceHosteleryViewModels:frmEmpresasVM/>
</ListBox.DataContext>
Al Data provides from a MySQL Database from both tables (empresas y empresas_ejercicios).
My goal is, when user select a item in listbox1 (empresas) show exercices in listbox2 (empresas_ejercicios).
If exist other path to determine this operation, i'm all eyes!!!
Thanks in advance.
Not sure if this is the only problem in your code, but what you're doing by setting
<ListBox.DataContext>
<InnovaCommerceHosteleryViewModels:frmEmpresasVM/>
</ListBox.DataContext>
for both list boxes is creating a separate instance of the ViewModel for each list box. Now I guess if you make any changes in the first list box' ViewModel instance, these changes will not be reflected in the second list box because this one binds to a totally different ViewModel instance.
Instead, try globally binding the ViewModel to the whole window / page / user control / etc. (depending on whether you;re doing WPF, Windows Phone, etc.), and let the list boxes inherit it to ensure that only one ViewModel instance is involved:
<Window.DataContext>
<InnovaCommerceHosteleryViewModels:frmEmpresasVM/>
</Window.DataContext>
EDIT:
Alternatively, you might as well instantiate the ViewModel once and store it as global resource:
<Window.Resources>
<InnovaCommerceHosteleryViewModels:frmEmpresasVM x:Key="Viewmodel" />
</Window.Resources>
and in the listboxes' DataContext just reference this global resouce:
<ListBox DataContext="{StaticResource Viewmodel}" ... />
I have the following view.xaml and I bind a collection(SavedTracksCollection from viewmodel) to this list box and it displays the items in UI.
<phone:PanoramaItem Name="MusicTracks" Header="Saved Tracks" >
<Grid>
<ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}" SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Background="Red" >
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding TrackTitle}"/>
<TextBlock Text="{Binding TrackUri}"/>
</StackPanel>
</Button>
<DataTemplate>
</ListBox.ItemTemplate>
</Grid>
</phone:PanoramaItem>
And the i have the following property defined in my viewmodel(this viewmodel is set as data context for my view) for the selecteditem binding "SelectedItemTrack".And i am binding SavedTracksCollection to the itemsource of the list.
private SavedTracksModel _SelectedItemTrack;
public SavedTracksModel SelectedItemTrack
{
get {
return _SelectedItemTrack;
}
set
{
if (value!=null)
_SelectedItemTrack = value;
//RaisePropertyChanged("SelectedItemTrack"); I dont think we need this.Let me know otherwise.
}
}
private List<SavedTracksModel> _SavedTracksCollection = new List<SavedTracksModel>();
public List<SavedTracksModel> SavedTracksCollection
{
get
{
return GetSavedTracks();
}
set
{
this._SavedTracksCollection = value;
RaisePropertyChanged("SavedTracksCollection");
}
}
But i am not able to determine how do i capture the SelectedITem event when user selectes an item from the Listbox .Currently it doesn't trigger the set method of the SelectedITemTrack .Once i capture the event with the details of selected item binding "TrackUri" i want to go to a new page where i can play the track.
any idea how to fix the issue ?
The first solution I can think of, why not just use the SelectionChanged event on ListBox?
<ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}"
SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}"
SelectionChanged="List_OnSelectionChanged"/>
// in code behind
private void List_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// navigate here after validating the selected item
// or raise Command in your ViewModel programatically
}
I am trying to bind an ItemsSource to a ComboBox in a RowDetailsTemplate. If I place a ComboBox outside the grid it works fine. I think this is occureing because of the ItemsSource property on the grid may be throwing off the ComboBox within the RowDetailsTemplate. XAML is below any thoughts?
Categories and CatTypes are two different ObservableCollections.
No error is occurring; the ComboBox just appears empty.
<ComboBox ItemsSource="{Binding CatTypes}"></ComboBox>
<my:DataGrid Name="gridProds" AutoGenerateColumns="False"
AlternatingRowBackground="Gainsboro" ItemsSource="{Binding Categories}">
<my:DataGrid.Columns>
<my:DataGridTextColumn x:Name="CatId" Header="CatID" Width="Auto" Binding="{Binding CategoryID}" />
<my:DataGridTextColumn Header="CatName" Width="Auto" Binding="{Binding CategoryName}" />
</my:DataGrid.Columns>
<my:DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label>ID:</Label>
<TextBox Name="txtGridCatId" Text="{Binding CategoryID}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Category Type:</Label>
<ComboBox ItemsSource="{Binding CatTypes}"></ComboBox>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</my:DataGrid.RowDetailsTemplate>
</my:DataGrid>
There is a class in the called DataSource in which the following is done:
private ObservableCollection<string> _cattypes = new ObservableCollection<string> { };
public ObservableCollection<string> CatTypes
{
get
{
_cattypes = new ObservableCollection<string> { };
SqlConnection con = new SqlConnection("MyConnStringHere;");
SqlCommand cmd = new SqlCommand("Select ID, CatType from PfCategoryType ORDER BY CatType", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string CatType = (string)rdr["CatType"];
_cattypes.Add(CatType);
}
con.Close();
return _cattypes;
}
}
In the MainWindow.xaml.cs I have:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataSource dataSource = new DataSource();
this.DataContext = dataSource;
}
}
If you checked the debug output in VS you would see the actual binding error. Most likely below code will fix it for you.
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=CatTypes}" />
If you can't get RelativeSource to work then use names. The property CatTypes is a property of some class which you created an object for and set as datacontext to some control. Just give that control a name (for example myControl) and bind like this:
<ComboBox ItemsSource="{Binding ElementName=myControl, Path=CatTypes}" />
If that don't work you need to post more of your code to figure out what you are doing wrong.
What happens if you try this?
<ComboBox DataContext="{Binding DataContext, ElementName=myControl}" ItemsSource="{Binding CatTypes}" />
(Of course you'd rename "myControl" to match the name of your window.)
Here, we're setting the data context of the combo box to be the same as the data context of the window. Since this is also the same data context of the first combo box in your XAML, I imagine the second combo box will start behaving like the first. (Although I worry that this will result in some unnecessary database connections, one per grid row.)
On second thought, if you need to set other properties in the context of the row, you won't want to set the data context of the entire ComboBox. In that case, I'd try something like this.
<ComboBox ItemsSource="{Binding ElementName=myControl, Path=DataContext.CatTypes}" SelectedItem="{Binding CategoryType}" />