简体   繁体   English

WPF绑定样式的TextBlock的Text属性

[英]Wpf Binding Text property of TextBlock from styles

I have a datagrid that include GridCell style template. 我有一个包含GridCell样式模板的数据网格。 I write this style template for that: 我为此编写了此样式模板:

<Style x:Key="SfDataGridGridCell" TargetType="syncfusion:GridCell">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="FontFamily" Value="IRANSansMobile(FaNum) Light"/>
                <Setter Property="FontSize" Value="11.5"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="syncfusion:GridCell">
                            <Border BorderBrush="Transparent">
                                <Grid>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding ???}"/>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

             </Style>

I want to apply this style for all DataGrid 's cells. 我想将此样式应用于所有DataGrid的单元格。 How can I Binding values that have been marked from xaml or code? 如何绑定从xaml或代码标记的值?

Perhaps what you need is ContentPresenter . 也许您需要的是ContentPresenter
If so, you can use ContentPresenter instead of TextBlock. 如果是这样,则可以使用ContentPresenter代替TextBlock。

<Grid>
    <!--<TextBlock TextWrapping="Wrap" Text="{Binding ???}"/>-->
    <ContentPresenter Content="{TemplateBinding Content}"/>
</Grid>

Please refer to here for how to apply styles to TextBlock in ContentPresenter. 请参阅此处以了解如何将样式应用于ContentPresenter中的TextBlock。
Applying style resources to ContentPresenter text 将样式资源应用于ContentPresenter文本

I am afraid you cannot replace just the path of the binding but reuse the rest of the template. 恐怕您不能仅替换绑定的路径,而要重用模板的其余部分。 XAML has no support for doing this. XAML不支持这样做。

What you could do instead is to create the several styles programmatically, one for each column, using the XamlReader.Parse method. 您可以做的是使用XamlReader.Parse方法以编程方式创建几种样式,每列一种。 Then you can simply replace the binding path in the string that you pass to the method. 然后,您可以简单地替换传递给方法的字符串中的绑定路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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