当组合框选择更改时,Xaml更改TextBlock的文本

时间:2016-03-10 10:35:30

标签: xaml combobox datatrigger

我目前在我的一个Xaml文件中遇到问题。我创建了一个带有2个固定组合框项目的combox。我还创建了一个文本块。这是xaml代码:

<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”的textBlock文本会发生变化。为了做到这一点,我在TextBlock中创建了两个绑定到Combobox Control的“Text”属性的数据触发器。根据Text属性的值,textblock的Text值会发生变化。

但它不起作用。即使我更改了我的组合框选择,也只显示默认值“Toto”。

任何帮助都将非常感谢:) :)

吉斯

1 个答案:

答案 0 :(得分:0)

避免设置Text的{​​{1}}属性。试试这个

TextBlock

如果要设置默认值,请按以下步骤

进行设置
<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>

希望这会有所帮助!!