English 中文(简体)
具有约束力的:框框框框框框号
原标题:binding:text block to listbox wpf
  • 时间:2012-05-10 11:32:05
  •  标签:
  • 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>

如果我选择名单上的一个项目,案文栏将不出现。 我认为问题在于选定项目。 页: 1 我如何解决这一问题?

最佳回答

你是正确的。 它应当是“途径”。 内容:

<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}" >
问题回答

一些建议

  • 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

至少,Shakati是受约束的,应当适用于选定的项目。 内容

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




相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签