0

I'm writing an application that is able to switch language using this. I also have a combox with two features:

  • supply help text if no item is selected in combobox (see this)
  • items are selectable with checkboxes (see this)

My combobox looks like

<Grid>
    <ComboBox x:Name="MyCB" SelectionChanged="OnCbObjectsSelectionChanged" ...>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsSelected}" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked"  Width="20" />
                    <TextBlock Text="{Binding Value}" Width="100" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <TextBlock Visibility="{Binding SelectedItem, ElementName=MyCB, Converter={StaticResource Null2Vis}}"
        IsHitTestVisible="False"
        VerticalAlignment="Center"
        Name="tbObjects"
        Text="{ns:Loc Var1}"
        FontSize="20"
        Foreground="Gray"/>
    </Grid>

I temporary deactivated the converter with return Visibility.Visible; with no effect.

Whenever I check some checkboxes the combobox.Text property is set and the binding from ns:Loc is overriden. How can I set it again in code if all checkboxes are unchecked?

private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e)
{
    StringBuilder sb = new StringBuilder();
    foreach (var cbObject in MyCB.Items)
        if (cbObject.IsSelected)
            sb.AppendFormat("{0}, ", cbObject.Value);
    tbObjects.Text = sb.ToString().Trim().TrimEnd(',');

    if (tbObjects.Text == "")
    {
        Binding myBinding = new Binding();
        myBinding.Source = TranslationSource.Instance["Var1"]; // <- this does not work :/
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        BindingOperations.SetBinding(tbObjects, TextBox.TextProperty, myBinding);

        tbObjects.Foreground = new SolidColorBrush(Colors.Gray);
    }
}

When all checkboxes are unchecked there is no text in the combobox. What am I missing?


Edit: Added TextBox element to code

Community
  • 1
  • 1
Andreas Sawatzki
  • 127
  • 2
  • 12

1 Answers1

0

If I understand your code correctly to restore the localization binding you don't need to redefine the Binding - it should be sufficient to use LocExtension with proper argument, because it derives from Binding:

if (tbObjects.Text == "")
{
    var myBinding = new LocExtension("Var1");
    BindingOperations.SetBinding(tbObjects, TextBox.TextProperty, myBinding);

    tbObjects.Foreground = new SolidColorBrush(Colors.Gray);
}

But if for some reason you still want to redefine the Binding, here's what it should look like:

if (tbObjects.Text == "")
{
    Binding myBinding = new Binding("[Var1]");
    myBinding.Source = TranslationSource.Instance;
    myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(tbObjects, TextBox.TextProperty, myBinding);

    tbObjects.Foreground = new SolidColorBrush(Colors.Gray);
}

Note that the source for the binding is TranslationSource.Instance, and the path should be "[Var1]" to indicate its indexer with "Var1" argument.

Side note

Since TranslationSource.Item[string] indexer is read-only, setting the Binding.UpdateSourceTrigger is reduntant. Also, for the same reason, I'd set Binding.Mode to OneWay.

Community
  • 1
  • 1
Grx70
  • 10,041
  • 1
  • 40
  • 55
  • Unfortunately none of the solutions work. The textbox stays empty after unchecking all checkboxes. Even if I set the text manually it doesn't change when I change the language. tbObjects.Text = TranslationSource.Instance["Var1"]; – Andreas Sawatzki Nov 17 '16 at 09:36
  • In that case it would be useful if you included the `TextBox` in question in your code - at this moment we can only see the `ComboBox`. – Grx70 Nov 17 '16 at 09:39
  • I added the `TextBox` – Andreas Sawatzki Nov 17 '16 at 09:49
  • What is `CBDefaultTarget`? And is its `SelectedItem` property `null`? Because otherwise the `TextBlock` is not visible (due to the binding on `Visibility` property). – Grx70 Nov 17 '16 at 10:10
  • Oh, sorry. `CBDefaultTarget`should be `MyCB`. I used simplified names for my question and overlooked that one. I temporary modified my converter and return `Visibility.Visible` without effect. – Andreas Sawatzki Nov 17 '16 at 10:23