How to change TextBlock Text Binding in C#

278 views Asked by At

I'm working in huge application and I have one small problem My Application has two languages (Arabic / English). I have ComboBox And I would like to change the display content according to the language.

This is my ComboBox XAML:

<ComboBox x:Name="cmbCustomerGroup" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="5" 
    Margin="2" SelectedValuePath="CustomerGroupId"  Validation.Error="Validation_Error"
    SelectedValue="{Binding Path=CustomerGroupId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}">
    <!--<ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>-->
</ComboBox>

This is my method:

private void FillCustomerGroups()
{
    var oClsCustomers = new ClsCustomerGroups();
    var lstCustGrps = oClsCustomers.GetData();
    cmbCustomerGroup.ItemsSource = lstCustGrps.ToList<TbCustomerGroups>();
    cmbCustomerGroup.DisplayMemberPath = Helper.CurrLang == Helper.SystemLanguage.Arabic ? "CustomerGroupAName" : "CustomerGroupEName";
    cmbCustomerGroup.SelectedValuePath = "CustomerGroupId";
}

I got this result:

result

This is my database:

database

1

There are 1 answers

1
RusArt On

This usually occurs when DisplayMemberPath is set wrong, or bindable property is not a string and has no overriden ToString() method.

Try add new property to your TbCustomerGroups, for example CurrentGroupName like this

 public string CurrentGroupName => Helper.CurrLang == Helper.SystemLanguage.Arabic ? CustomerGroupAName : CustomerGroupEName;

Then set cmbCustomerGroup.DisplayMemberPath = "CurrentGroupName"

Also check that CustomerGroupAName and CustomerGroupEName are strings or have ToString() method

UPDATE

Also don't use <ComboBox.ItemTemplate> if you use DisplayMemberPath