朋友们,我是WPF MVVM结构的新手。 我试图使用Trigger将TextField值复制到Label。我正在做的是在xaml代码下面。
VehicalForm.xaml
<Window x:Class="Seris.VehicalForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="600">
<Control>
<Control.Template>
<ControlTemplate>
<WrapPanel Orientation="Vertical" Margin="10 " >
<Label Content="Vehical No" HorizontalAlignment="Left"/>
<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo}" HorizontalAlignment="Left" />
<Label Content="Model" HorizontalAlignment="Left"/>
<TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model}" HorizontalAlignment="Left" />
<Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
<DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, Mode=TwoWay}"/>
<Label Content="IU No" HorizontalAlignment="Left"/>
<TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo}" HorizontalAlignment="Left"/>
<Label Content="Personnel" HorizontalAlignment="Left"/>
<ComboBox Name="Personnel_Combo" SelectedValue="{Binding PersonnelNameSelected}" ItemsSource="{Binding PersonnelName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Width="116"/>
<Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
<Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
<Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>
<ListView Name ="Grid" Height="294" Width="371" >
<DataGrid Name="DG" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="Cell" GridLinesVisibility="None" IsReadOnly="True" AutoGenerateColumns="False" BorderThickness="0">
<DataGrid.Columns>
<DataGridTextColumn Header="Vehical No" Binding="{Binding VehicalNo}" />
<DataGridTextColumn Header="Model" Binding="{Binding Model}" />
<DataGridTextColumn Header="ManufacturingDate" Binding="{Binding ManufacturingDate}" />
<DataGridTextColumn Header="IUNo" Binding="{Binding IUNo}" />
<DataGridTextColumn Header="Personnel" Binding="{Binding PersonnelNameSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
</ListView>
<Label Name="Notification" Content="hjgj"/>
</WrapPanel>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=VehicalNo_Text}" Value="true">
<Setter Property="Content" TargetName="Notification" Value="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
</Window>
在这里,我无法绑定&#34; Vehical_Text&#34;标签命名为&#34;通知。 所以我的问题是,
1)如何使用触发器成功完成此操作 2)我找不到IsMouseClick。如果我需要mouseClick事件设置哪个属性。
答案 0 :(得分:0)
对于绑定部分,您可以尝试
<Label Name="Notification"
Content="{Binding ElementName=VehicalNo_Text,
Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
关于触发器部分,您指的是标签?如果是这样,你可以试试这些:
MouseDoubleClick="someEvent"
MouseDown="anotherEvent"/>
更新:
以msdn为例。
需要为控件设置名称:
<Control Name="someControl">
然后在触发时访问控件内的元素,
private void whatEverControl_MouseDown(object sender, MouseButtonEventArgs e)
{
var text2paste = (sender as TextBox).Text;
var target = (Label)someControl.Template.FindName("Notification", someControl);
target.Content = text2paste;
}
答案 1 :(得分:0)
您可以在XAML中声明文本框事件处理程序使用预览事件(即PreviewMouseDown,PreviewMouseLeftButtonDown等)
<强>的Sample1 强>
<StackPanel>
<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" HorizontalAlignment="Left" >
<TextBox.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Notification" Storyboard.TargetProperty="Content">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding ElementName=VehicalNo_Text,Path=Text}"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
<Label Name="Notification"/>
</StackPanel>
<强>样品2 强>
<StackPanel>
<TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" HorizontalAlignment="Left" >
<TextBox.Triggers>
<EventTrigger RoutedEvent="TextBox.MouseDoubleClick">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Notification" Storyboard.TargetProperty="Content">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding ElementName=VehicalNo_Text,Path=Text}"></DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
<Label Name="Notification" Content="change content"/>
</StackPanel>