2016-03-15 47 views
2

我得到了一個LED燈和一個TextBlock的按鈕,LED燈將根據一些規則而改變。當我在DataTamplate中使用它時,一切正常。當我想將其移動到ResourceDictionay時,我不知道如何設置TextBlock的文本。如何更改按鈕上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}"的文本?

回答

3

這是你所需要的:

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

你救我的天!謝謝 ! – Whistler

相關問題