开发者

Getting text from textblock defined in style

开发者 https://www.devze.com 2023-03-11 01:08 出处:网络
I have a style in my Silverlight 4 app. where I define column names in a grid, is it possible to get these te开发者_如何学编程xt of the headers from an object with this style? In the code below, what

I have a style in my Silverlight 4 app. where I define column names in a grid, is it possible to get these te开发者_如何学编程xt of the headers from an object with this style? In the code below, what I would like to get is the strings "foo" and "bar", from an object with this style applied.

</Grid>
    <Grid
    x:Name="m_Headers"
    Visibility="Visible"
    Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition
            Width="{StaticResource DataColunmWidth}" />
            <ColumnDefinition
            Width="{StaticResource DataColunmWidth}" />
        </Grid.ColumnDefinitions>
        <TextBlock
        Margin="3,0,0,0"
        Text="foo"
        Grid.Column="1" />
        <TextBlock
        Margin="3,0,0,0"
        Text="bar"
        Grid.Column="2" />
    </Grid>


I don't think you can do this with pure XAML, you'd have to duplicate the templates or create a custom control which exposes the text values as dependency properties via the template.

The best solution I think is to set up bindings for your Grid headers and fill them using your ViewModel:

<Grid
x:Name="m_Headers"
Visibility="Visible"
Grid.Row="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition
        Width="{StaticResource DataColunmWidth}" />
        <ColumnDefinition
        Width="{StaticResource DataColunmWidth}" />
    </Grid.ColumnDefinitions>
    <TextBlock
    Margin="3,0,0,0"
    Text="{Binding FooText}"
    Grid.Column="1" />
    <TextBlock
    Margin="3,0,0,0"
    Text="{Binding BarText}"
    Grid.Column="2" />
</Grid>
0

精彩评论

暂无评论...
验证码 换一张
取 消