如何在C#中更改TextBlock文本绑定

时间:2022-09-02 13:24:44

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.

我正在大量申请,我有一个小问题我的应用程序有两种语言(阿拉伯语/英语)。我有ComboBox我想根据语言更改显示内容。

This is my ComboBox XAML:

这是我的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:

我得到了这个结果:

如何在C#中更改TextBlock文本绑定

This is my database:

这是我的数据库:

如何在C#中更改TextBlock文本绑定

1 个解决方案

#1


0  

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

当DisplayMemberPath设置错误,或者bindable属性不是字符串且没有覆盖ToString()方法时,通常会发生这种情况。

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

尝试将新属性添加到您的TbCustomerGroups,例如CurrentGroupName

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

Then set cmbCustomerGroup.DisplayMemberPath = "CurrentGroupName"

然后设置cmbCustomerGroup.DisplayMemberPath =“CurrentGroupName”

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

还要检查CustomerGroupAName和CustomerGroupEName是字符串还是具有ToString()方法

UPDATE

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

如果使用DisplayMemberPath,也不要使用

#1


0  

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

当DisplayMemberPath设置错误,或者bindable属性不是字符串且没有覆盖ToString()方法时,通常会发生这种情况。

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

尝试将新属性添加到您的TbCustomerGroups,例如CurrentGroupName

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

Then set cmbCustomerGroup.DisplayMemberPath = "CurrentGroupName"

然后设置cmbCustomerGroup.DisplayMemberPath =“CurrentGroupName”

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

还要检查CustomerGroupAName和CustomerGroupEName是字符串还是具有ToString()方法

UPDATE

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

如果使用DisplayMemberPath,也不要使用