c# - WPF绑定(bind)(使用触发器)不能从文本框工作到标签

标签 c# wpf xaml mvvm

friend 们,我是 WPF MVVM 结构的新手。
我试图使用触发器将 TextField 值复制到标签。我正在做的是在下面的 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>

在这里,我无法将“Vehical_Text”的值绑定(bind)到名为“通知”的标签。
所以我的问题是,

1)如何使用触发器成功地做到这一点
2) 我找不到 IsMouseClick。如果我需要 mouseClick 事件来设置哪个属性。

最佳答案

You can declare your textbox-event-handlers in XAML Using Preview-events (i.e. PreviewMouseDown,PreviewMouseLeftButtonDown etc)



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

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

关于c# - WPF绑定(bind)(使用触发器)不能从文本框工作到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110440/

相关文章:

c# - 除了强制转换 IntPtr 来增加它之外,还有其他方法吗?

c# - 当从Visual Studio(而不是ClickOnce发布版本)运行C#WPF应用程序时,PowerPoint文件打开

wpf - 查看控件显示/隐藏 WPF MVVM

xaml - DataTemplate 中 ViewMode 的 WinRT 绑定(bind)数据

c# - UWP 调用 ContentDialog 作为使用依赖注入(inject)的服务

xaml - Windows 8 应用程序 : Fixed button width: Failed to create a 'Windows.Foundation.Double' from the text '1in' ?

c# - 将自定义动词添加到 ToolStrip 设计器,而无需重新实现 ToolStripDesigner

C#读取ifc/BIM文件序列化-反序列化

c# - 模型未验证

c# - 相同的 XAML,但用户控件背后的代码不同