I've built a custom ComboBox which shows as a TextBox when ReadOnly is set:
<local:BoolToVisibilityConverter FalseValue="Hidden" TrueValue="Visible" x:Key="BoolVis" />
<local:BoolToVisibilityConverter FalseValue="Visible" TrueValue="Hidden" x:Key="BoolVisRev" />
<Style TargetType="{x:Type local:ComboBoxG}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ComboBoxG}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<ComboBox ItemsSource="{TemplateBinding ItemsSource}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
SelectedValuePath="{TemplateBinding SelectedValuePath}"
SelectedIndex="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedIndex, Mode=TwoWay}"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsReadOnly, Converter={StaticResource BoolVisRev}}"
IsDropDownOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
IsTabStop="False">
</ComboBox>
<TextBox Text="{TemplateBinding Text}"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsReadOnly, Converter={StaticResource BoolVis}}"
IsTabStop="False">
</TextBox>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It's working fine, other than if I instantiate and set a value all in one go. This doesn't work ...
private void One_OnClick(object sender, RoutedEventArgs e)
{
cmb = new ComboBoxG();
Stack.Children.Add(cmb);
var dict = new Dictionary<int, string> { { 0, "aaa" }, { 1, "bbb" }, { 2, "ccc" }, { 3, "ddd" }, };
cmb.ItemsSource = dict;
cmb.DisplayMemberPath = "Value";
cmb.SelectedValuePath = "Key";
cmb.SelectedValue = 3;
}
... whereas this does ...
private void One_OnClick(object sender, RoutedEventArgs e)
{
cmb = new ComboBoxG();
Stack.Children.Add(cmb);
var dict = new Dictionary<int, string> { { 0, "aaa" }, { 1, "bbb" }, { 2, "ccc" }, { 3, "ddd" }, };
cmb.ItemsSource = dict;
cmb.DisplayMemberPath = "Value";
cmb.SelectedValuePath = "Key";
cmb.Loaded += cmb_Loaded;
}
private void cmb_Loaded(object sender, RoutedEventArgs e)
{
cmb.SelectedValue = 3;
}
So I can work around it but it is making it awkward for some uses of this control. Any suggestions, please?