WPF ComboBox IsEnabled from Items Count - wpf

Im wanting the XAML of a ComboBox to handle its own IsEnabled property based on whether or not it has Items. If the DataTable in the code behind returns Items, id like the ComboBox to be enabled otherwise if no Items are added, it remains or becomes a disabled control. Is this at all possible?
My current ComboBox setup:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}" />

If you want the ComboBox to be enabled when it has items, and disabled when there are no items, you could just bind IsEnabled to the HasItems property:
<ComboBox x:Name="ImportDate"
DisplayMemberPath="FileDate"
SelectedValuePath="ID"
ItemsSource="{Binding Mode=OneWay}"
SelectedIndex="0"
Style="{DynamicResource sanComboBox_Standard}"
IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />

Related

Cannot change appearance of "{NewItemPlaceholder}" in WPF combobox

I have bound an ObservableCollection<Company> to a combobox.
<ComboBox
IsEditable="True"
Text="{Binding Path=Company.CompanyName, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=CompanyCollection,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ucEventDialog}}}"
SelectedItem="{Binding Path=Company}"
HorizontalAlignment="Stretch"/>
During runtime WPF adds at the end of the bound list an additional ComboboxItem {NewItemPlaceHolder} to the dropdown list.
I would like to use this item to trigger an additional dialog box to add a new item to the collection.
I tried to create a template for this additional item using <ComboBox.Resources> ...
<ComboBox.Resources>
<DataTemplate x:Key="PlaceholderTemplate">
<TextBlock Text="New..."/>
</DataTemplate>
</ComboBox.Resources>
However I have not found any means to ...
Change the appearance of the {NewItemPlaceHolder}.
Trigger an event when the {NewItemPlaceHolder} is selected.

WPF DataGrid ItemsSource - Change from ComboBox

Here is a snippet of XAML:
<ComboBox ItemsSource="{Binding UnileverDataSet.Tables, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Margin="5" x:Name="TableNameComboBox">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TableName}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding UnileverDataSet.Tables[TableNameComboBox.SelectedIndex]}"
UnileverDataSet is a DataSet made up of about 12 DataTables
The idea here is that when the ComboBox value changes, the DataGrid should update based on the index value from the ComboBox.
Is this possible or should I look at another way of doing this?
If I do: UnileverDataSet.Tables[0], then all works and data displays correctly.
You can do element binding with combobox..
Your combox will display list of items in the "UnileverDataSet.Tables" collection. When ever your select a item in Combo box the selected item will bounded to the Datagrid Items source (since we are using element binding)
Here is the sample code
<DataGrid Margin="5" AutoGenerateColumns="True" ItemsSource="{Binding SelectedItem,ElementName=TableNameComboBox}">

How do I bind to different property on ComboBox SelectedItem?

I have a combobox in WPF like this:
<ComboBox Text="Select Language..." IsEditable="True" IsReadOnly="True"
ItemsSource="{Binding XPath=item/#name, Source={StaticResource Items}}"
SelectedItem="{Binding Path=Test, Mode=OneWayToSource}"/>
Where Items is:
<XmlDataProvider x:Key="Items" Source="/itemlist.xml" XPath="/itemlist"/>
Test is a property of type object on ViewModel that is set as datacontext of a window.
Everything works fine, and my Test property receives XmlNode object, which makes sense.
However, I would like to receive different attribute from that xml for example XPath=item/#value
How do I do that?
Use DisplayMemberPath and SelectedValuePath:
<ComboBox Text="Select Language..." IsEditable="True" IsReadOnly="True"
ItemsSource="{Binding XPath=item, Source={StaticResource Items}}"
DisplayMemberPath="#name"
SelectedValuePath="#id"
SelectedValue="{Binding Path=Test, Mode=OneWayToSource}"/>
The selected item will be the item element, it will display the name attribute, and it will bind the id attribute to Test.

Binding a WPF ComboBox to a different ItemsSource within a ListBox DataTemplate

I have a ListBox that contains a textbox and a combobox in its datatemplate:
<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Name}" />
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to bind the listbox to a collection of objects (which I've done successfully), but I want the combobox in the above datatemplate to have its itemssource set to a local property on the window (array of ints). I still want the combobox to have a two-way bind between its selected item and a property on the collection of objects...
I have the following in code:
PitcherListBox.DataContext = this;
Basically in the end, I want the combobox within the listbox to have a different itemssource than the listbox itself. I can't seem to figure out how to change the ComboBox's ItemsSource in XAML. Can someone provide me some feedback? Thanks!
Try this:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
Note that you need to replace YourWindowTypeHere with the type of the Window containing the LocalArrayOfIntsProperty! Also remember that you will need to define an xml namespace for that type!

BInding for Listbox In WPF

I have a Main window with textbox. I enter some text and click enter it popups a window with listbox and displays the items matching the text entered in the Mainwindow. On selecting the item from the ListBox the text gets set in my textbox of Mainwindow.
I am following the MVVM Pattern. I am not able to set the binding for my listbox in my Mainwindow.(Using CommandBinding)
Does anyone has some solution or sample for the similar scenario?
Thanks
On the ViewModel you need a property SelectedListBoxItem
Bind it to the SelectedItem of your ListBox.
Bind the Text-property to the SelectedListBoxItem of your ViewModel
and that should basically be it.
<ListBox
ItemsSource="{Binding Path=ItemsView, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
SelectedItem="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
/>
<TextBox Text="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
/>

Resources