Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何绑定文本块';s值与同一窗口中的另一个TextBlocks值?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何绑定文本块';s值与同一窗口中的另一个TextBlocks值?

C# 如何绑定文本块';s值与同一窗口中的另一个TextBlocks值?,c#,wpf,xaml,C#,Wpf,Xaml,请看下面的图片。这是一个窗口,我在网格中放置了多个TextBlocks。实际上是营销团队迫使我这样设计UI。但我还面临其他问题,比如 我有这样的XAML代码,因为只有这样放置TextBlocks才能实现UI设计 <TextBlock x:Name="BlueTextBlock" Margin="100,10,0,0" Foreground="Blue" Text="{Binding ElementName=GridAlltex

请看下面的图片。这是一个
窗口
,我在
网格
中放置了多个
TextBlock
s。实际上是营销团队迫使我这样设计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
的值与网格内
textblock
的值之一绑定

假设我选择了具有text=“
值2
”的文本块,那么
BlueTextBlock
文本也应该是
值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>
更多的例子

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


但ComboBox无法解决UI设计问题。更重要的是Ui。否则,可以使用ListBox实现相同的功能,并将属性绑定到选定的ListBox项。但实际上我的UI设计一团糟。这也可以通过
GridView
来完成,但是如果没有一些代码,简单地选择一个可聚焦的元素是不可行的。。。因此在那一点上没有绑定。通常,您在ViewModel中会有一个属性,
TextBox
TextBlock
都绑定了它们的
Text
属性。但是,您谈论的是焦点(selected?)和绑定到selected项。然后使用
ItemsControl
(或
ListBox
),因为它们有
SelectedItem
属性,您可以将
TextBox.Text
绑定到该属性(实际上,最好在ViewModel中有属性,该属性保存您绑定到的
SelectedItem
ViewModel)。是的,我可以,我同意这一点,但如果我这样做了,我如何才能以这种方式放置列表框项目?对我来说,UI应该是这样的,我可以有任何类型的实现。:)ListBox项控制可以绑定到ViewModel中某些属性的项。但是如何使用ListBox或ListView设计此UI?要使布局与图片一样,请使用
UniformGrid
as
ListBox.ItemsPanel
,请参阅。或者更好(它会创建2列,您需要3列)。感谢您的回答,我们将对此进行研究并在此处更新。
<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>