I have a WPF ComboBox which is databound to a Collection, but depending on wether a Checkbox is checked or not I'd like to vary which Collection the ComboBox is bound to.
The basic issue is I have a large collection of MyCustomer and I also have a filtered collection of MyCustomer - the filtering is quite intensive and I don't want to do it with a CollectionView for the main reason that it is already done, the filtered collection already exists - hence the need to simply switch the databinding of the combo.
I'm hoping for a pure XAML solution, obviously writing some code behind would be a relatively simple solutions but it doesn't feel like it should be required.
Here's an example using a DataTrigger to switch the collections:
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel.Resources>
<x:Array x:Key="notes" Type="{x:Type sys:String}">
<sys:String>do</sys:String>
<sys:String>re</sys:String>
<sys:String>mi</sys:String>
</x:Array>
<x:Array x:Key="letters" Type="{x:Type sys:Char}">
<sys:Char>a</sys:Char>
<sys:Char>b</sys:Char>
<sys:Char>c</sys:Char>
</x:Array>
</StackPanel.Resources>
<CheckBox x:Name="chkLetters" Content="Use Letters"/>
<ListBox>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Setter Property="ItemsSource" Value="{StaticResource notes}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=chkLetters}" Value="True">
<Setter Property="ItemsSource" Value="{StaticResource letters}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>
</StackPanel>
For you these wouldn't be different arrays, but probably different CollectionViewSources with filters or something, but the principle is the same.
Best way I know is to use some shell collection that internally 'gets' the right collection.
So you have your UnfilteredCollection and your FilteredCollection and then a property called BindingCollection which, in its 'getter,' evaluates some state (the checkbox would be bound to this state) to determine which collection to retrieve.
If you use MVVM for the databinding between the UI and the collections, one way to do it would be like this:
<!-- Your ComboBox binds to some shell collection -->
<ComboBox ItemsSource="{Binding BindingCollection}" />
<!-- The input to this item will determine which collection is internally exposed -->
<CheckBox IsChecked="{Binding UseFilteredSet}" />
And then have your ViewModel (middle-layer) file do something like this (I'm not including the implementation details of INotifyPropertyChanged, but I can if you wish):
private ObservableCollection<MyCustomer> UnfilteredCollection
{
get { return _unfilteredCollection; }
}
private ObservableCollection<MyCustomer> FilteredCollection
{
get { return _filteredCollection; }
}
// The public collection to which your ComboBox is bound
public ObservableCollection<MyCustomer> BindingCollection
{
get
{
return UseFilteredSet ?
FilteredCollection :
UnfilteredCollection;
}
}
// CheckBox is bound to this state value, which tells the bindings on the shell
// collection to refresh when the value of this state changes.
public bool UseFilteredSet
{
get { return _useFilteredSet; }
set
{
_useFilteredSet = value;
OnPropertyChanged("UseFilteredSet");
OnPropertyChanged("BindingCollection");
}
}
Related
This question already has answers here:
Binding usercontrol property to custom class
(1 answer)
Creating generalized user controls with MVVM Light
(1 answer)
Datacontext conflicts
(1 answer)
Closed 4 years ago.
I have a WPF form with a content control and a custom control. The content control swaps in views based on a radio button selection. Once the user takes an action on the view, I set the nocustomer on the parent viewmodel (the WPF form containing the two controls) to false. When this occurs, the visibility of content control correctly disappears. Unfortunately, the visibility of the custom control remains unchanged (it should have also disappeared). I'm actually perplexed because in my mind they have the exact same implementation and therefore should behave the same.
<ContentControl x:Name="ViewSwap" Content="{Binding SearchingViewModel}"
Visibility="{Binding NoCustomer, Converter={StaticResource
BooleanToVisibilityConverter}, Mode=OneWay}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource AddressTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<views:CTACallSubmit x:Name="CallSubmit"
Visibility="{Binding NoCustomer, Converter={StaticResource
BooleanToVisibilityConverter}, Mode=OneWay}"/>
Update:
MainWindow's DataContext
public partial class CTALight : Window
{
public CTALight()
{
InitializeComponent();
this.DataContext = CTALightViewModel.GetInstance();
}
}
MainViewModel
public class CTALightViewModel : ObservableObject
{
public static CTALightViewModel _mainViewModel;
public static CTALightViewModel GetInstance()
{
if (_mainViewModel == null)
_mainViewModel = new CTALightViewModel();
return _mainViewModel;
}
private CTALightViewModel()
{
}
}
CTACallSubmit DataContext
<UserControl.DataContext>
<viewmodel:CTACallSubmitViewModel />
</UserControl.DataContext>
The following creates a new instance of CTACallSubmitViewModel and sets the DataContext of the UserControl to this one.
<UserControl.DataContext>
<viewmodel:CTACallSubmitViewModel />
</UserControl.DataContext>
This means that the binding to the NoCustomer property of the other view model won't work unless you specify a source of the binding:
<views:CTACallSubmit x:Name="CallSubmit"
Visibility="{Binding DataContext.NoCustomer,
RelativeSource={RelativeSource AncestorType=Window},
Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"/>
Setting the DataContext of a UserControl like this is usually a bad idea as it breaks the inheritance of the parent's DataContext.
I have a Window with two tabs, that holds two different user controls. In order to enable/disable navigation to the second tab, I implement an IsEnabled property in both VM's from the IPageViewModel interface.
The IsEnabled boolean property is set to true when a SelectedCustomer is received in the CustomerOrdersViewModel, via Messenger service from CustomerDetailsViewModel.
So far this method works, as the second tab is enabled when I select a customer from the data grid in the first view. But the problem is when I try to select the first tab to go back to the initial view, it is disabled.
This is a screen cast of the specific navigation issue.
I'm not sure why as I thought when I set the IsEnabled property to true using the messenger, both tabs would be enabled.
Does anyone have any advice on the issue here?
In the CustomerDetailsViewModel I send the selectedCustomer via a messenger:
private CustomerModel selectedCustomer;
public CustomerModel SelectedCustomer
{
get
{
return selectedCustomer;
}
set
{
selectedCustomer = value;
Messenger.Default.Send<CustomerModel>(selectedCustomer);
RaisePropertyChanged("SelectedCustomer");
}
}
Then in the CustomerDetailsViewModel the IsEnabled property is set to true as the SelectedCustomer has been passed over:
public CustomerOrdersViewModel()
{
Messenger.Default.Register<CustomerModel>(this, OnCustomerReceived);
}
public void OnCustomerReceived(CustomerModel customer)
{
SelectedCustomer = customer;
IsEnabled = true;
}
This is the ApplicationView xaml that holds both user controls, and the tabs generated for each:
<Window x:Class="MongoDBApp.Views.ApplicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MongoDBApp.Views"
xmlns:vm="clr-namespace:MongoDBApp.ViewModels"
Title="ApplicationView"
Width="800"
Height="500">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:CustomerDetailsViewModel}">
<views:CustomerDetailsView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerOrdersViewModel}">
<views:CustomerOrdersView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:ApplicationViewModel />
</Window.DataContext>
<TabControl ItemsSource="{Binding PageViewModels}"
SelectedItem="{Binding CurrentPageViewModel}"
TabStripPlacement="Top">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Window>
Why not just default the IsEnabled property of the CustomerDetailsViewModel to true?
It's a tab that should always be enabled, so that would make the most sense to me.
I presume you assign new ViewModel, either CustomerDetails or CustomerOrders, to CurrentPageViewModel. Whenever you do so a new object of class is created with IsEnabled set to false by default.
The work around is to create IsEnabled property in ViewModel associated with your View (ApplicationViewModel). Then in ItemContrainerStyle refer to it as follows:
<Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource AncestoryType=Window}, Path=DataContext.IsEnabled}"/>
Neither IsEnabled nor Messages are required in TabControlPages' ViewModels since your IsEnabled property resides in main ViewModel and both TabPages refer to it.
EDIT
After a while I realized that it would be disable by default since IsDefault would equal false at very beginning. It is complicated because you do not explicitly create TabPage. I am attaching complete solution for this, take a look
here.
I have and DataGridComboBoxColumn in the DataGrid a WPF window. I am assigning DataContext to Window as below:
cls = new MyClass
{
selValue = 2,
DataGrid = dtGrid,
ComboGrid = dtCombo
};
this.DataContext = cls;
Following is the XAML for DataGridComboBoxColumn:
<DataGridComboBoxColumn Header="Item Name" SelectedValueBinding="{Binding Path=Item_Id}" SelectedValuePath="ItemId" DisplayMemberPath="ItemName">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<!-- modified this code as per suggestion ///-->
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.ComboGrid }" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.ComboGrid }" />
</Style>
</DataGridComboBoxColu
mn.EditingElementStyle>
Still Combobox in grid showing blank. No data is being listed in Combobox. Then, I wrote following code in Windows codebehind, it start working.
((DataGridComboBoxColumn)this.testGrid.Columns[1]).ItemsSource = cls.ComboGrid.DefaultView;
Is there anyway to handle this case in XMAL itself using MVVM? I am reluctant to use this apporache.
If the itemsSource is not within the datagrids itemssource you will have to find ancestor:
<DataGridComboBoxColumn itemsSource="{binding RelativeSource={RelativeSource ancestortype=Page}, path=DataContext.YourComboboxItemsSource}" />
Assuming your datagrid is on a page, you can change the ancestortype to anything. You can use relativeSource on anything though. The reason for having to use this is that the itemssource you are trying to set is not part of the hierarchy so it can't find it. Hope this helps.
MVVM I would do something like:
public list<string> ComboboxGridItemsSource { get; set; }
//Then add some data in the property above.
ComboboxGridItemsSource.add("Hello world1"); , ect...
And when this list gets altered/updated remember to raise the property using INotifyPropertyChanged.
//After you implement INotifyPropertyChanged you can raise like this:
RaiseProperty("ComboboxGridItemsSource");
Using MVVM you generally wouldn't manually set properties directly to the control but rather bind properties to that control in xaml.
According to msdn, it should be perfectly legal, and possible, to bind something to a nested property:
<Binding Path="propertyName.propertyName2" .../>
<Binding Path="propertyName.propertyName2.propertyName3" .../>
In my case, it's not so, though...
I have a custom control, MyControl, with a dependency property ViewModel:
public static DependencyProperty ViewModelProperty = DependencyProperty.Register(
"ViewModel", typeof(IViewModel), typeof(MyControl));
public IViewModel ViewModel
{
get { return (IViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
and in the control template, I try to bind to properties in that viewmodel:
<Style TargetType="{x:Type my:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:MyControl}">
<Grid>
<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>
<Button x:Name="MyButton" Content="Visible by trigger" Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.ButtonVisible}" Value="True">
<Setter TargetName="MyButton" Property="Visibility" Value="Visible" />
</DataTrigger>
.../>
In the viewmodel itself, I have a preoperty Text as follow:
public string Text
{
get { return m_text; }
set
{
m_text = value;
OnPropertyChanged("Text");
}
}
public bool ButtonVisible
{
get { return m_buttonVisible; }
set
{
m_buttonVisible = value;
OnPropertyChanged("ButtonVisible"); }
}
I get no bind errors, but things doesn't happend...
Any clues?
Edit
It looks like the bindings work half way. When the text is changed in the editbox, my Text property is set, but if the Text-property is set in code, the ui won't update.
Edit 2
Looks like my first attempt at simplifying the case before posting was a little to successful... As #Erno points out, the code that I posted seems to work OK.
I have looked at the original code some more, and added a trigger to the scenario. The original code uses triggers to show parts of the ui at given conditions. These are also binded to nested properties. I now think that these triggers fail to trigger. I have updated the code. If it still doesn't show whats wrong, I can post a sample application some where.
There is a comma missing:
<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text}"/>
EDIT
Add Mode=TwoWay to the binding:
<TextBox Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Text, Mode=TwoWay}"/>
EDIT2
Got it! I could reproduce and fix it.
Replace the TemplatedParent with Self in the binding.
Read this explanation
I want to display a custom template/item as selected item in ComboBox (this item does not actually exist in the list of items and is updated differently). This does not even needs to be an item, just providing a custom view would work.
How can I do this while staying within current ComboBox theme (so no ControlTemplate replacement possible)? As far as I see, all of SelectionBox* properties are not editable and internally ComboBox uses unnamed ContentPresenter.
I would do it like this:
<Window.Resources>
<DataTemplate x:Key="NormalItemTemplate" ...>
...
</DataTemplate>
<DataTemplate x:Key="SelectionBoxTemplate" ...>
...
</DataTemplate>
<DataTemplate x:Key="CombinedTemplate">
<ContentPresenter x:Name="Presenter"
Content="{Binding}"
ContentTemplate="{StaticResource NormalItemTemplate}" />
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor,ComboBoxItem,1}}"
Value="{x:Null}">
<Setter TargetName="Presenter" Property="ContentTemplate"
Value="{StaticResource SelectionBoxTemplate}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
...
<ComboBox
ItemTemplate="{StaticResource CombinedTemplate}"
ItemsSource="..."
... />
The reason this works is that CombinedTemplate normally just uses NormalItemTemplate to present its data, but if there is no ComboBoxItem ancestor it assumes it is in the selection box so it uses SelectionBoxTemplate.
Note that the three DataTemplates could be included in any level of ResourceDictionary (not just at the Window level) or even directly within the ComboBox, depending on your preference.
If I have this straight, you want a control that has something arbitrary displayed along with a drop-down button that displays a list of items with checkboxes next to them?
I wouldn't even bother trying to restyle a ComboBox to achieve this. The problem is that ComboBox is more specialized down a different path than what you need. If you look at the ComboBox ControlTemplate Example, you'll see that it simply uses a Popup control to display the list of possible values.
You can take pieces of that template as guidance to creating a UserControl that is easier to understand and better provides what you want. You'll even be able to add a SelectedItems property and such that ComboBox doesn't provide.
An example of what I mean by guidance: the Popup has an IsOpen property. In the control template, it's set to {TemplateBinding IsDropDownOpen}, which means that the ComboBox class has an IsDropDownOpen property that is changed in order to control the expand/collapse of the Popup.
Alexey Mitev's comment on Ray Burns' answer inspired me to write the following reasonably short utility class, which I now use in all my WPF projects:
public class ComboBoxItemTemplateSelector : DataTemplateSelector
{
public List<DataTemplate> SelectedItemTemplates { get; } = new List<DataTemplate>();
public List<DataTemplate> DropDownItemTemplates { get; } = new List<DataTemplate>();
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
return GetVisualParent<ComboBoxItem>(container) == null
? ChooseFrom(SelectedItemTemplates, item)
: ChooseFrom(DropDownItemTemplates, item);
}
private static DataTemplate ChooseFrom(IEnumerable<DataTemplate> templates, object item)
{
if (item == null)
return null;
var targetType = item.GetType();
return templates.FirstOrDefault(t => (t.DataType as Type) == targetType);
}
private static T GetVisualParent<T>(DependencyObject child) where T : Visual
{
while (child != null && !(child is T))
child = VisualTreeHelper.GetParent(child);
return child as T;
}
}
With that in the toolbox, it's possible to write XAML like this:
<UserControl.Resources>
<DataTemplate x:Key="SelectedItemTemplateForInt" DataType="{x:Type system:Int32}">
<!-- ... -->
</DataTemplate>
<DataTemplate x:Key="SelectedItemTemplateForDouble" DataType="{x:Type system:Double}">
<!-- ... -->
</DataTemplate>
<DataTemplate x:Key="DropDownItemTemplateForInt" DataType="{x:Type system:Int32}">
<!-- ... -->
</DataTemplate>
<DataTemplate x:Key="DropDownItemTemplateForDouble" DataType="{x:Type system:Double}">
<!-- ... -->
</DataTemplate>
</UserControl.Resources>
<ComboBox>
<ComboBox.ItemTemplateSelector>
<local:ComboBoxItemTemplateSelector>
<local:ComboBoxItemTemplateSelector.SelectedItemTemplates>
<StaticResource ResourceKey="SelectedItemTemplateForInt" />
<StaticResource ResourceKey="SelectedItemTemplateForDouble" />
</local:ComboBoxItemTemplateSelector.SelectedItemTemplates>
<local:ComboBoxItemTemplateSelector.DropDownItemTemplates>
<StaticResource ResourceKey="DropDownItemTemplateForInt" />
<StaticResource ResourceKey="DropDownItemTemplateForDouble" />
</local:ComboBoxItemTemplateSelector.DropDownItemTemplates>
</local:ComboBoxItemTemplateSelector>
</ComboBox.ItemTemplateSelector>
</ComboBox>
You need to look into Triggers and Styles. You might also want to look into some of my older questions here on StackOverflow that helped me conquer these problems:
Displaying Content only when ListViewItem is Selected
Using Styles in Windows Presentation Foundation