2016-03-10 84 views
0

我目前在我的一個Xaml文件中遇到問題。我創建了2個固定組合框項目的combox。我也創建了一個文本塊。這裏是XAML代碼:Xaml當組合框選擇變化時更改TextBlock的文本

<StackPanel> 
    <TextBlock Grid.Column="0" x:Name="UserSettingsConnectorGroupBoxProductTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Strings.UserSettingsConnectorGroupBoxProductText, Source={StaticResource StringLocalizer}}" VerticalAlignment="Center" Margin="10,0,0,0" /> 
    <ComboBox Grid.Column="1" x:Name="UserSettingsConnectorGroupBoxProductComboBox" VerticalAlignment="Center" Width="300" HorizontalAlignment="Left" Margin="10,5,0,0" SelectionChanged="UserSettingsConnectorGroupBoxProductComboBox_SelectionChanged" > 
     <ComboBoxItem Content="Microsoft Deployment Toolkit" /> 
     <ComboBoxItem Content="Microsoft System Center Configuration Manager" /> 
    </ComboBox> 
    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Margin="10,0,0,0"> 
    <TextBlock Name="ConnectorTextBlock" Text="toto" Margin="0,5" > 
     <TextBlock.Style> 
      <Style TargetType="TextBlock"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft Deployment Toolkit"> 
         <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathMDT, Source={StaticResource StringLocalizer}}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft System Center Configuration Manager"> 
         <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathSCCM, Source={StaticResource StringLocalizer}}" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 
    <StackPanel Orientation="Horizontal" > 
     <TextBox Name="ConnectorTextBox" Margin="0,5" Width="300"> 
     </TextBox> 
     <Button Content="Test" Margin="5" Width="100" HorizontalAlignment="Right"/> 
    </StackPanel> 
    <Button Content="Save" Width="100" HorizontalAlignment="Left" Margin="0,5" IsEnabled="False"/> 
    </StackPanel> 

和預覽:

enter image description here

我想文字塊的名爲 「ConnectorTextBox」 改變ComboBox中選定項目發生變化時的文字。爲了做到這一點,我在TextBlock中創建了2個綁定到「組合框控件」的「文本」屬性的數據觸發器。根據Text屬性的值,文本塊的文本值發生變化。

但它不起作用。即使我更改我的組合框選擇,也只顯示默認值「Toto」。

任何幫助,將不勝感激:) :)

紅塔

+0

注意:我使用相同的過程來將其他控件的可見性和IsEnabled屬性設置爲文本框和按鈕,並且它運行得很完美......似乎問題僅出現在我的文本塊控件 –

回答

0

避免設置的TextBlockText財產。試試這個

<TextBlock Name="ConnectorTextBlock" Margin="0,5" > 
    <TextBlock.Style> 
     <Style TargetType="TextBlock"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft Deployment Toolkit"> 
        <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathMDT, Source={StaticResource StringLocalizer}}" /> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft System Center Configuration Manager"> 
        <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathSCCM, Source={StaticResource StringLocalizer}}" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </TextBlock.Style> 
</TextBlock> 

如果你想設置的默認值,這樣做如下

<TextBlock Name="ConnectorTextBlock" Margin="0,5" > 
       <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Setter Property="Text" Value="Toto" /> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft Deployment Toolkit"> 
           <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathMDT, Source={StaticResource StringLocalizer}}" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding ElementName=UserSettingsConnectorGroupBoxProductComboBox, Path=Text}" Value="Microsoft System Center Configuration Manager"> 
           <Setter Property="Text" Value="{Binding Strings.UserSettingsConnectorGroupBoxProductTextBlockConnectorPathSCCM, Source={StaticResource StringLocalizer}}" /> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 

希望這有助於!

+0

上我遵循了您的建議,我在正確的方式。實際上,DataTrigger可以很好地運行,但是這不起作用的本地化。如果我用一個固定值替換文本的值,它可以完美運行。 現在我必須明白爲什麼本地化不會返回任何價值。 非常感謝您的幫助! –

+0

是的,我也測試了一個固定值。 – Subru

+0

我終於找到了問題。我沒有正確設置本地化值:-) –

相關問題