0

我是 WPF 的新手。我有这个代码

<Window x:Class="ElementBinding.MultipleBindings"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MultipleBindings" Height="320" Width="300">
    <Grid>
        <Slider Name="sliderFontSize" Margin="0,12,6,243"
                Minimum="1" Maximum="40" Value="10"
                TickFrequency="1" TickPlacement="TopLeft">
        </Slider>
        <TextBox Name="txtContent" Margin="0,44,0,208">Color should change here</TextBox>
        <TextBlock Margin="3,150,3,3" Name="lblSampleText"
                    FontSize="{Binding ElementName=sliderFontSize, Path=Value}"
                    Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
                    Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Tag}" >
        Multiple Bindings
        </TextBlock>
        <ListBox Height="54" HorizontalAlignment="Left" Margin="12,90,0,0" Name="lstColors" VerticalAlignment="Top" Width="120" >
            <ListBoxItem>Green</ListBoxItem>
            <ListBoxItem>Red</ListBoxItem>
            <ListBoxItem>Blue</ListBoxItem>
        </ListBox>
    </Grid>
</Window>

如果我在列表框中选择一个项目,文本块将不会出现。我认为问题出在“SelectedItem.Tag”中。我该如何解决这个问题?

4

2 回答 2

2

你是对的。它应该是 Path=SelectedItem.Content:

<TextBlock Margin="3,150,3,3" Name="lblSampleText"
                FontSize="{Binding ElementName=sliderFontSize, Path=Value}"
                Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
                Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Content}" >
于 2012-05-10T11:47:46.700 回答
2

一些建议

  • 不要只使用 Margin 来排列控件,查看 Layout with Panels(Grid、DockPanel 等)
  • 不要使用字体大小和滑块来创建缩放之类的东西,最好使用带有 ScaleTransform 的 LayoutTransform

至少 Shakti 是正确的,绑定应该是 SelectedItem.Content

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Slider Name="sliderFontSize" Grid.Row="0" TickPlacement="TopLeft"
            Minimum="1" Maximum="5" Value="1"
            TickFrequency="1">
    </Slider>
    <TextBox Name="txtContent" Grid.Row="1">Color should change here</TextBox>
    <TextBlock Grid.Row="3" Name="lblSampleText"
                Text="{Binding ElementName=txtContent, Path=Text,Mode=TwoWay}"
                Foreground="{Binding ElementName=lstColors, Path=SelectedItem.Content}" >
        <TextBlock.LayoutTransform>
            <ScaleTransform ScaleX="{Binding ElementName=sliderFontSize, Path=Value}" ScaleY="{Binding ElementName=sliderFontSize, Path=Value}"/>
        </TextBlock.LayoutTransform>

    </TextBlock>
    <ListBox Height="54" HorizontalAlignment="Left"  Name="lstColors" VerticalAlignment="Top" Width="120" Grid.Row="2">
        <ListBoxItem>Green</ListBoxItem>
        <ListBoxItem>Red</ListBoxItem>
        <ListBoxItem>Blue</ListBoxItem>
    </ListBox>
</Grid>
于 2012-05-10T11:55:10.143 回答