简体   繁体   中英

How to access textblock inside Datatemplate in combobox?

I am trying to Bind the TextBlock inside ComboBox through Code. I am able to bind the textblock sucessfully but For some reasons TextBlock doesnt display Text Values.

I have mechanism which checks for the selected values and there I am getting the selected values without any problem.

So to conclude, My binding is working fine but I am missing out something hence textblock is not displaying text that is bound with it.

This is the code I am using for Binding: where "lObjTextBlock" is TextBlock inside of ComboBox.

TextBlock lObjTextBlock = (TextBlock)ComboBox.ItemTemplate.LoadContent();

 Binding lObjBinding = new Binding();

lObjBinding.Path = new PropertyPath("[" + lObjMap.PropertyName + "]");

lObjTextBlock.SetBinding(TextBlock.TextProperty, lObjBinding);

This is the XAML for the TextBlock:

<my:HComboBox Name="cmbRefDoctor">
<my:HComboBox.ItemTemplate>
    <DataTemplate>
                <TextBlock x:Name="txtRefDoctorName" />
    </DataTemplate>
</my:HComboBox.ItemTemplate>
</my:HComboBox>

Once again : My problem is that TextBlock is not displaying any Text althought values are being set.

Would love to get all possible suggestions. Thanks in advance.

it's one of the way to bind controls which inside datatemplate

this.DataContext = Person;

Binding binding = new Binding();
binding.Source = ob;
DataTemplate dtemp = (DataTemplate)Resources["PointTemp"];
Border bdr = dtemp.LoadContent() as Border;
TextBlock tblk = bdr.Child as TextBlock;
tblk.SetBinding(TextBlock.TextProperty, binding);

Here I used ob as double collection assign to textproperty by binding source

<UserControl.Resources>
    <DataTemplate x:Key="PointTemp">
        <Border Margin="0,23,0,0" Background="Transparent">
            <TextBlock Text="{Binding}"  Foreground="White" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center"/>
        </Border>
    </DataTemplate>
</UserControl.Resources>

And you can assign,

if it's combobox or listbox

Here Person is a class name, or set the class name in combobox itemsource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM