我是WPF和MVVM结构的初学者。以下是我查看部分和相关文件的代码:
ListOfVehicle
<Window x:Class="Seris.ListOfVehicle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListOfVehcle" Height="600" Width="700"
xmlns:cnv="clr-namespace:Seris.Converters">
<Window.Resources>
<cnv:ID2Name x:Key="converter" />
</Window.Resources>
<Grid HorizontalAlignment="Center">
<Label Content="Manage Vehicle" HorizontalAlignment="Left" Height="27" Margin="261,8,0,0" VerticalAlignment="Top" Width="103" FontWeight="Bold" FontSize="12"/>
<Label Content="SERIS CAD" HorizontalAlignment="Left" Height="30" Margin="53,8,0,0" VerticalAlignment="Top" Width="84" FontWeight="Bold"/>
<Menu x:Name="ListOfPersonnnel" HorizontalAlignment="Left" Height="32" Margin="10,35,0,0" VerticalAlignment="Top" Width="603">
<MenuItem Header="Manage Vehicle >>" />
</Menu>
<Button Name="Add_Button" CommandParameter="add" Command="{Binding OpenAddWindow_Command}" Content="Add" Height="28" Width="81" Margin="246,396,315,46"/>
<Button Name="Replace_Button" CommandParameter="replace" Command="{Binding ReplaceButton_Command}" IsEnabled="{Binding IsEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="Replace" Height="28" Width="81" Margin="345,396,216,46"/>
<Button Name="Remove_Button" CommandParameter="remove" Command="{Binding RemoveButton_Command}" IsEnabled="{Binding IsEnableReplaceButton, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="Remove" Height="28" Width="81" Margin="442,396,119,46"/>
<ListView Name ="Grid" Margin="104,67,185,226" >
<DataGrid Name="DG" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedRow, Mode=TwoWay}" SelectionMode="Single" GridLinesVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Vehicle No" Binding="{Binding VehicleNo}"/>
<DataGridTextColumn Header="Model" Binding="{Binding Model}" />
<DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
<DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
<DataGridTextColumn Header="Personnel" Binding="{Binding PersonnelNameSelected, Converter={StaticResource converter} }" />
<DataGridTextColumn Header="Unique No" Binding="{Binding UniqueNo}"/>
</DataGrid.Columns>
</DataGrid>
</ListView>
</Grid>
</Window>
做完这些后,我陷入了困境。我需要的是如下,但不知道如何实施。
假设我将2个TextBox放在View部分和一个Label中。 (分别考虑TextBox1,TextBox2和Label)
我需要的是WPF C#的一个非常小的例子,这样当我点击特定的TextBox时,它会在Label中提供内容以及该文本框的名称。
即。名称是&#39; TextBox1&#39;,内容是&#39;你好吗?&#39;
答案 0 :(得分:-3)
使用Triggers和StringFormat可以帮助你做到这一点..
<Grid>
<ContentControl>
<ContentControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Name="myTextBlock"/>
<TextBox Name="TextBox1"></TextBox>
<TextBox Name="TextBox2"></TextBox>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=TextBox1}" Value="true">
<Setter Property="Text" TargetName="myTextBlock">
<Setter.Value>
<MultiBinding StringFormat="{}Name is '{0}', Content is '{1}'">
<Binding ElementName="TextBox1" Path="Name" />
<Binding ElementName="TextBox1" Path="Text" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=TextBox2}" Value="true">
<Setter Property="Text" TargetName="myTextBlock">
<Setter.Value>
<MultiBinding StringFormat="{}Name is '{0}', Content is '{1}'">
<Binding ElementName="TextBox2" Path="Name" />
<Binding ElementName="TextBox2" Path="Text" />
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Grid>