WPF ComboBox IsEnable when using custom DataTemplate - wpf

Here it's the following. I want to disable a combo box.
In some scenarios in a window I just set SelectedItem from ViewModel to something and don't allowing the user to change it. But in some cases I want to allow.
This one works perfectly when changing ComboBoxIsEnabled property in VM.
<ComboBox ItemsSource="{Binding MyColletionView, Mode=OneWay}"
SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
IsEnabled="{Binding MyComboBoxIsEnabled}"/>
After changing ComboBox.ItemTemplate the IsEnable property not reacting anymore. In short, I can't disable the ComboBox.
<ComboBox ItemsSource="{Binding MyColletionView, Mode=OneWay}"
SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding MyComboBoxIsEnabled}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource CustomerConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I don't want to disable the ComboBoxItem, I want to disable the ComboBox itsef.
Any suggestions, or someone facing the same issue?

<ComboBox Height="20" Width="200" IsEnabled="True" Margin="38,38,1682,1022" ItemsSource="{Binding SourceData}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I write a sample as you described.but I didnot use mvvm.I want to prove that there is no relationship in IsEnable and ItemTemplate.
you can trace this issue from tips as follow.
Watch Output window in VisualStudio.There may be some exception.
replace mvvm with codebehind and try.

Related

The tapped on comboBox work incorrectly

I have comboBox with names list . When I try to select a name in the list, it generally takes 3-5 clicks on the selected name.
<ComboBox
Grid.Row="6"
Height="50"
Margin="0,20,0,0"
Name="AssetClassComboBox"
HorizontalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{x:Bind ListAssetClass, Mode=OneWay}">
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="models:ComboBoxAssetClassItem">
<ComboBoxItem Content="{x:Bind Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If I delete this code the comboBox works correctly, the list is visible, but I cannot see the names.
<ComboBox.ItemTemplate>
<DataTemplate x:DataType="models:ComboBoxAssetClassItem">
<ComboBoxItem Content="{x:Bind Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
Instead of creating a ItemTemplate you could simply use the property DisplayMemberPath of ComboBox:
<ComboBox
...
DisplayMemberPath="Name"
ItemsSource="{x:Bind ListAssetClass, Mode=OneWay}">

WPF DataGrid SelectedItem Command not firing

I've got a question about wpf datagrid an the behaviour of a DatagridTemplateColumn.
<DataGrid
ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
inf:MultiSelectorBehaviours.SynchronizedSelectedItems="{Binding SelectedItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectionUnit="FullRow" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Background="Transparent"
BorderThickness="0"
BorderBrush="Transparent"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ExecuteCommand}" CommandParameter="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
My Question is: When i press the button in DataGridTemplateColumn and the row is NOT selected, command is fired in my model. But when select my row first, command is not fired. Does anybody know why?
Thanks for your help...
EDIT: When i change SelectionMode from "Extended" to "Single", it is working as expected. But i have to use "Extended"...
Try IsSynchronizedWithCurrentItem="True" on your datagrid. It's a common cause of why "SelectedItem" related issues arise. Otherwise you should provide a more complete code sample to get a solution to this.

ListBoxItem not selected when CheckBox is checked

I have a WPF ListBox which contains a CheckBox as follow:
<ListBox x:Name="MyListBox"
Grid.Row="1"
ItemsSource="{Binding Path=Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=ReceiveNewsletter}"
Margin="0,3,0,0"
IsTabStop="False"/>
<TextBlock Text="{Binding Path=FirstName}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But I am having an issue about ListBoxItem highlighting. When I click the CheckBox, the ListBoxItem does not get highlighted.
Can anyone give me some idea how to solve the issue?
Thanks
If the checkbox should correspond to the selection you can bind the selection to the same property you bound the checkbox to, do so in the ListBox.ItemContainerStyle using a Setter for IsSelected.

How to trigger DataGridevents DataGridBeginningEdit, DataGridCellEditEnding with a Combobox in Silverlight? / CellTemplate for a Combobox

I want to use the DataGridevents (DataGridBeginningEdit, DataGridCellEditEnding, ..etc) to handle and detect changes. As far as I understood, without a "CellTemplate" these are not triggered. So I am trying to create an appropriate celltemplate using a TextBlock, but I guess it is not very straightforward with the binding I am using for the Combobox in the CellEditingTemplate, because I am using "DisplayMemberPath"..
There are examples of simpler cases but I couldn't find smth for this scenario. See Xaml snippet below;
<data:DataGridTemplateColumn Width="100">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox HorizontalAlignment="Stretch"
ItemsSource="{Binding DurationTypeList, Source={StaticResource itemSourceProvider}}"
SelectedValuePath="Code"
SelectedValue="{Binding Path=DurationTypeCode, Mode=TwoWay}"
DisplayMemberPath="Template" />
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
Thank you
It turns out, i have two options..
Solution #1
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Text="{Binding Path=DurationType.Template, Mode=OneWay}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox HorizontalAlignment="Stretch"
ItemsSource="{Binding DurationTypeList, Source={StaticResource itemSourceProvider}}"
SelectedValuePath="Code"
SelectedValue="{Binding Path=DurationType, Mode=TwoWay}"
DisplayMemberPath="Template" />
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
I changed the binding path from string to the object with Code and Template properties..
This blog helped a lot..

C# WPF two different DataContext in One Control while using MVVM and the secound property is every time the same

First the code:
<ListView ItemsSource="{Binding DataTransferModel.Output}" Background="Transparent" Margin="0" VerticalContentAlignment="Top" AlternationCount="2" Name="lvOutput" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,1">
<UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}"
IsReadOnly="True"
Grid.Row="2"
TextWrapping="WrapWithOverflow"
SelectedValue="{Binding Path=DataContext.SelectedOutput,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}
}"
/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And the problem is that the property Data on the OutputTextBox control comes from the list, but the property SelectedOutput should come from the main DataContext the ViewModel. And the property SelectedOutput should be the same for every entry in the list. But currently it dosn't work. :(
does your listview is enclosed in tags and if yes does the grid has the DataContext from the ViewModel? if yes it should work.
do you see any binding errors in your output window? maybe you should try using Snoop to see your real binding of SelectedValue.
EDIT: Please try a Type other then Grid, maybe ListView if it has the ViewModel DataContext.
<UserControls:OutputTextBox Text="{Binding Data, Mode=OneWay}"
IsReadOnly="True"
Grid.Row="2"
TextWrapping="WrapWithOverflow"
SelectedValue="{Binding Path=DataContext.SelectedOutput,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}
}"
/>

Resources