I've added triggers for combobox, i.e.
When combo box is 'Disabled' then 'Text' property should set to '1'
and added updatesourcetrigger to property changed.
When combo box is 'Enabled' then 'Text' property should be selected one and added
update source trigger to property changed.
But the problem is I'm unable to call the update source trigger when combo box is Disabled and 'Text' property is set to '1'.
Below is the XAML code snippet:
<ComboBox Name="cmbIntervals"
Grid.Row="5"
Grid.Column="1"
Width="150"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsEnabled="{Binding ElementName=chkRollingInterval,
Path=IsChecked}"
ItemsSource="{Binding Source={x:Static es:MasterParameters.Instance},
Path=NoOfIntervals}"
Tag="{Binding Path=[NoOfIntervals], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Text" Value="1"/>
<Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag, Mode=TwoWay, UpdateSourceTrigger=Default}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
From above XAML code when ever i updated 'Text' property to '1' for combo box 'Disabeld' mode i need to update this '1' value to the source property i.e. 'Tag' and from there to 'NoOfIntervals' but it doesn't happening.
Thanks,
Nag
Related
I have created a combo box in each datagrid row. The following piece of code is used to create the combo box:
<ComboBox Width="166"
ItemTemplate="{StaticResource GridBinding}"
SelectedItem="{Binding Path=Car, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
SelectedValue="{Binding Path=Car, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}">
<ComboBox.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource GroupHeader}" />
</ComboBox.GroupStyle>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=Cars}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=DataGridCell}}" Value="True">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.GroupedCars, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
The "Car" property for binding "SelectedItem" in combo box is an object of class "Car" holding some properties like id, name, etc.
The problem I am facing is that when I update the value of "Car" property and call "NotifyPropertyChanged" in its setter, then the value of "SelectedItem" in combo box goes blank/empty.
Please suggest.
The SelectedItem can no longer be found in the Collection (when you update your ItemSource) and gets set to null.
I've simplified your XAML to demonstrate
<ComboBox ItemsSource="{Binding Cars}"
SelectedItem="{Binding Car}">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="SelectedItem" Value="{x:Null}">
<Setter Property="SelectedIndex" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Now the first Item will get selected when you update.
I've to change the Text property of a combo box when IsEnabled sets to false and it can manually selects field from the items source when IsEnabled is true from the same control, is this possible?
XAML:
<ComboBox Name="cmbIntervals"
Grid.Row="5"
Grid.Column="1"
Width="150"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsEnabled="{Binding ElementName=chkBox,
Path=IsChecked}"
ItemsSource="{Binding Source={x:Static res:Parameters.Instance},
Path=Intervals}"
Text="{Binding [Intervals], Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" >
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Text" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
Just check in ViewModel
if(!IsChecked)
{
TextProperty="1"; //Propery to bind to ComboBox Text
}
My combobox is bound to a list of states. The state have a column participating which when true the comboitem foreground color should be red. This works fine. But when I select the combobox item with foreground color red, it loses that foreground color and sets it to black. Can somebody help me point out what I am doing wrong?
<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
DisplayMemberPath="StateName"
ItemsSource="{Binding States}"
SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Foreground" Value="{Binding DoesNotParticipate, Converter={StaticResource NonParticipatingConverter}}"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
The binding you set for your ComboBoxes Foreground prop is looking for a ComboBoxItem typed ancestor in the visual tree, but the item you need is the descendant of the ComboBox. Especially the ComboBoxes SelectedItem.
EDIT
Since you bind the item to model objects the ComboBox.SelectedItems type would be this model objects type. Which means - probably - they don't have Foreground property.
I recommend the following:
It seems to me that DoesNotParticipate is a boolean prop so don't use Converter, use instead Trigger:
<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
DisplayMemberPath="StateName"
ItemsSource="{Binding States}"
SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.DoesNotParticipate}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger
</Style.Triggers>
</Style>
</ComboBox.Style>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<DataTrigger Property={Binding DoesNotParticipate} Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
I have a ComboBox and a TextBox and want to enable/disable TextBox only, when first item is selected in ComboBox.
This code works: (disable when first item is selected)
<ComboBox SelectedIndex="{Binding Mode}">
<ComboBoxItem>Mode 1</ComboBoxItem>
<ComboBoxItem>Mode 2</ComboBoxItem>
<ComboBoxItem>Mode 3</ComboBoxItem>
</ComboBox>
<TextBox Text="{Binding ValueNotForMode1}">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding ="{Binding Mode}" Value="0">
<Setter Property="TextBox.IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
This one doesn't work: (enable when first item is selected)
<ComboBox SelectedIndex="{Binding Mode}">
<ComboBoxItem>Mode 1</ComboBoxItem>
<ComboBoxItem>Mode 2</ComboBoxItem>
<ComboBoxItem>Mode 3</ComboBoxItem>
</ComboBox>
<TextBox IsEnabled="False" Text="{Binding ValueForMode1}">
<TextBox.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding ="{Binding Mode}" Value="0">
<Setter Property="TextBox.IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Isn't it possible to enable a control via DataTrigger?
Or how can I tell the control, what to do, when the Trigger not occures?
Move IsEnabled="False" from your TextBox to your TextBox.Style.
<Setter Property="TextBox.IsEnabled" Value="False" />
See Dependency Property Value Precedence for clearer understanding.
I can't seem to get the correct combination to get the desired effect:
Current XAML:
<Button Content="Foo" prism:Click.Command="{Binding FooCommand}"
Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />
<Button Content="Bar" prism:Click.Command="{Binding BarCommand}"
Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />
I want to extract out the Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" as a style to apply over all the Buttons (within this UserControl resources). I can't seem to get the correct combination going here to make that happen.
Basically, what it does is instead of just disabling the button based on the ICommand.CanExecute it takes that DependencyProperty and binds it to the Visibility of the Button using a boolean-visiblity converter so the button is not only disabled, but also collapsed.
Style would look like this i suppose:
<Style TargetType="{x:Type Button}">
<Setter Property="Visibility"
Value="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}"/>
</Style>
Doesn't that work?
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" Value="Visible">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>