请查看下图。这是一个Window
,我在TextBlock
内放置了多个Grid
。它实际上是营销团队,迫使我设计这样的UI。但我正面临其他问题,如
我有XAML
这样的代码,因为如果我以这种方式放置TextBlocks
,我只能实现UI设计。
<TextBlock x:Name="BlueTextBlock"
Margin="100,10,0,0"
Foreground="Blue"
Text="{Binding ElementName=GridAlltextBlocks, Path=??? Mode=TwoWay}"/>
<Grid x:Name="GridAlltextBlocks">
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Focusable="True" Text="Value 1"/>
<TextBlock Grid.Row="1" Grid.Column="0" Focusable="True" Text="Value 6"/>
<TextBlock Grid.Row="2" Grid.Column="1" Focusable="True" Text="Value 2"/>
<TextBlock Grid.Row="3" Grid.Column="3" Focusable="True" Text="Value 3"/>
<TextBlock Grid.Row="1" Grid.Column="1" Focusable="True" Text="Value 4"/>
<TextBlock Grid.Row="2" Grid.Column="3" Focusable="True" Text="Value 5"/>
</Grid>
现在的问题是:我如何将BlueTextBlock
的值与网格中textBlocks
的值之一绑定。
假设我选择了textBlock,其中text =“Value 2
”,那么BlueTextBlock
文字也应该是Value 2
。
我怎样才能做到这一点?
答案 0 :(得分:2)
以下是您需要的完整解决方案...非常容易将所选项目样式删除,您可以覆盖ListView.ItemContainerStyle
<Window x:Class="WpfApplication1.GridView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="ListView" Height="600" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="3"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Foreground="Blue" Height="45" Grid.Row="0" Margin="10" Text="{Binding ElementName=ListView, Path=SelectedItem}" FontSize="20"/>
<Rectangle Grid.Row="1" Fill="Gray" Margin="10 0"/>
<ListView x:Name="ListView" Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Items}" Foreground="Green" FontSize="20" Grid.IsSharedSizeScope="True" BorderThickness="0">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="Group" Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" FontSize="20" Foreground="Green" HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
</Window>
首先,TextBlock无法选择...如果您有一些可以选择的控件,例如组合框
您可以执行以下操作
Text="{Binding ElementName=ComboBoxName, Path=SelectedItem.Property}"
<TextBlock Text="{Binding ElementName=box1, Path=SelectedItem.Property}" Height="20"/>
<ComboBox Name="box1" Height="30" Margin="30" ItemsSource="{Binding Items}"/>
更多例子
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding ElementName=box1, Path=SelectedItem}" Height="20"/>
<ListView Name="box1" ItemsSource="{Binding Items}" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>