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

いくつかのアドバイス

  • マージンだけを使用してコントロールを配置しないでください。パネル (グリッド、ドックパネルなど) を使用したレイアウトを見てください。
  • フォントサイズとスライダーを使用してズームのようなものを作成しないでください。ScaleTransform で LayoutTransform を使用することをお勧めします。

少なくともシャクティは正しいバインディングは 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 に答える
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 に答える