如何更改按钮上TextBlock的文本?

时间:2016-03-15 12:12:05

标签: wpf xaml

我有一个带led灯和TextBlock的按钮,led灯将根据一些规则改变。当我在DataTamplate中使用它时,一切正常。当我想将它移动到ResourceDictionay时,我不知道如何设置TextBlock的文本。

<views:BaseView.Resources>
    <views:SwapBooleanValueConverter x:Key="SwapBooleanValueConverter" />
    <DataTemplate x:Key="FlowStagesTemplate">
        <StackPanel>
            <Button x:Name="TurulStageButton"
                    Tag="{Binding ID}"
                    Command="{Binding DataContext.OnButtonClickCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                    CommandParameter="{Binding ElementName=FlowStageButton}"
                    Style="{Binding FlowStageDisplayStyle}">
                <StackPanel Orientation="Horizontal" Width="200">
                    <Rectangle Width="4" Height="30" Fill="#64dd17" Margin="0,0,10,1" RadiusX="2" RadiusY="2"/>
                    <TextBlock Text="{Binding FlowStageName}" VerticalAlignment="Center" FontSize="14" Foreground="White" TextWrapping="WrapWithOverflow"/>
                </StackPanel>
            </Button>
        </StackPanel>
    </DataTemplate>
</views:BaseView.Resources>
<StackPanel Background="#263238">
    <ListView ItemsSource="{Binding FlowStagesSubMenu}" ItemTemplate="{StaticResource FlowStagesTemplate}" 
              BorderThickness="0" Background="#263238" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</StackPanel>

所以现在我想把它移到DictionaryResources:

    <Style x:Key="ButtonStyleGreen" TargetType="{x:Type Button}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Width="200">
                    <Rectangle Width="4" Height="30" Fill="#64dd17" Margin="0,0,10,1" RadiusX="2" RadiusY="2"/>
                    <TextBlock VerticalAlignment="Center" FontSize="14" Foreground="White" TextWrapping="WrapWithOverflow"/>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>

那么如何将TextBlock的文本绑定到Text="{Binding FlowStageName}"

1 个答案:

答案 0 :(得分:3)

这就是你需要的:

<TextBlock Text="{Binding Path=DataContext.FlowStageName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" />