SHRI
SHRI

Reputation: 2466

binding:text block to listbox wpf

I am new to WPF. I have this code

<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>

Text block will not appear if I select an item in list box. I think problem is in 'SelectedItem.Tag'. How can I resolve this?

Upvotes: 0

Views: 635

Answers (2)

blindmeis
blindmeis

Reputation: 22445

some advices

  • dont use just Margin to arrange your controls, look at Layout with Panels(Grid,DockPanel, and so on)
  • dont use the fontsize and a slider to create something like a zoom, better use LayoutTransform with ScaleTransform

at least Shakti is right the binding should be to 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>

Upvotes: 2

Shakti Prakash Singh
Shakti Prakash Singh

Reputation: 2533

You are correct. It should be 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}" >

Upvotes: 2

Related Questions