简体   繁体   中英

DataTemplate Binding to multiple object properties

I'm using button styling, with data template, and having problem with showing text (Name and Number properties of my Table objects).

This is xaml:

<Style x:Key="btnTable" TargetType="{x:Type Button}" >
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type my:Tables}">
                    <Grid>
                        <Image VerticalAlignment="Center" Source="/Images/Ico/table-40x40.png" />
                        <TextBlock Text="{Binding Path=Naziv}" />
                        <Ellipse Canvas.Top="30" Canvas.Left="30" Fill="#FF6A4E8C" />
                        <TextBlock Text="{Binding Number}" />
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Then, in .cs I create buttons like this:

    Button b = new Button();
    Style myStyle = (Style)Resources["btnTable"];
    b.Style = myStyle;
    b.DataContext = myTableItem;

If I test buttons click event, it has in DataContext property valid Table object.

I have tried replacing Binding Path width Binding Path=DataContext.Name, adding RelativeSource=Self, but nothing helps.

Additionally, I tried creating buttons using ItemsControl, but have the same issue.

b.Content = myTableItem;

It's the ContentTemplate . It's a template for the Content .

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