Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Dynamic Combobox isselected

$
0
0

I have a combobox that has the period of the day as its items. I am trying to make the isSelected item based on the period on the day through the use of a converter but for some reason my code won't work.

Below is my xaml:

<Window.Resources><staticData:SelectedPeriodConverter  x:Key="SelectedPeriodConverter"/>

<ComboBoxItem>AM</ComboBoxItem><ComboBoxItem>PM</ComboBoxItem><ComboBox.ItemContainerStyle><Style><Style.Triggers><DataTriggerBinding="{Binding Path=Content, RelativeSource={RelativeSource Self}, Converter={StaticResource SelectedPeriodConverter}}"Value="True"><SetterProperty="ComboBoxItem.IsEnabled"Value="False"/></DataTrigger></Style.Triggers></Style></ComboBox.ItemContainerStyle></ComboBox>

and my c# code for the converter is as follows:

publicclassSelectedPeriodConverter:IValueConverter{publicobjectConvert(object values,Type targetType,object parameter,CultureInfo culture){string test = values.ToString();if(test ==DateTime.Now.ToString("tt")){returntrue;}else{returnfalse;}}publicobjectConvertBack(object value,Type targetTypes,object parameter,CultureInfo culture){return value;}}

The weird thing is is if I change isSelected to IsEnable it will trigger but otherwise it won't.

Another way I tried was to have the styling in the Windows resources.

This worked if I targetted comboboxitem but would not work if I added an x:class and used ItemContainerStyle to isolate the trigger to one combobox as I did not want it working on all the comboboxes I had in my form.

<StyleTargetType="{x:Type ComboBoxItem}"><Style.Triggers><DataTriggerBinding="{Binding Path=Content, RelativeSource={RelativeSource Self}, Converter={StaticResource SelectedPeriodConverter}}"Value="True"><SetterProperty="IsSelected"Value="True"/></DataTrigger></Style.Triggers></Style><ComboBoxGrid.Column="4"Margin="0,7"Width="100"HorizontalAlignment="Right"Name="PeriodPicker"VerticalAlignment="Top"Height="25"SelectedItem="PM"><ComboBoxItem>AM</ComboBoxItem><ComboBoxItem>PM</ComboBoxItem></ComboBox>

Does anyone know how I can get this to work?

thanks Callum


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>