C#Eventhandler,参数控件类型来自xaml

C#Eventhandler,参数控件类型来自xaml,c#,wpf,xaml,eventhandler,C#,Wpf,Xaml,Eventhandler,我想调用onClick事件并将Textbox对象从.xaml代码传输到eventhandler。 有可能吗 我试着说明一下 这是我的Main.xaml.cs代码: <Button x:Name="VideoTargetBtn" Content="Browse..." HorizontalAlignment="Left" VerticalAlignment="Top" Width="89" Click="VideoTargetBtn_Click" Height="27" Margin="1

我想调用onClick事件并将Textbox对象从.xaml代码传输到eventhandler。 有可能吗

我试着说明一下

这是我的Main.xaml.cs代码:

<Button x:Name="VideoTargetBtn" Content="Browse..." HorizontalAlignment="Left" VerticalAlignment="Top" Width="89"  Click="VideoTargetBtn_Click" Height="27" Margin="10,13,0,0"/>
<TextBox x:Name="videoTargetPathTxt" Height="27" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True"  Margin="117,13,21,0"/>

如果您只需要从代码后面访问该控件,则只需按名称引用它,因为您在控件上使用的x:name属性的值为videoTargetPathTxt

让事件处理程序保持正常,并将代码替换为

if (fileIsSelected == true)
{
    videoTargetPathTxt.Text = saveFileDialog.FileName;
}

不,不是,你也不需要

在专用VideoTargetBtn_单击处理程序中,仅检索文本框内容

videoTargetPathTxt.Text


private void VideoTargetBtn_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();

    bool? fileIsSelected = saveFileDialog.ShowDialog();

    if (fileIsSelected == true)
    {
        videoTargetPathTxt.Text = saveFileDialog.FileName;
    }
}

每个按钮都有其他的文本框。一般来说,如果你在WPF中使用点击处理程序,你是做错了。我试图说明,这不是我的代码。不管是你的代码,这都是错误的。您不应该在WPF期间使用单击处理程序。查找
MVVM
ICommand
WPF
。这应该通过
命令
命令参数={Binding ElementName=videoTargetPathTxt,Path=Text}
@TalShaked来实现。我实现了一个解决方案,向您展示了下面的MVVM模型,它向您展示了一些很酷的技巧,并允许用户点击enter来调用您的方法。为了简单起见,我不使用ViewModelLocator,所以将datacontext设置在某个地方。IE用于在codebehind或直接在xaml中进行测试。。。或者使用viewmodellocator=)
videoTargetPathTxt.Text


private void VideoTargetBtn_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();

    bool? fileIsSelected = saveFileDialog.ShowDialog();

    if (fileIsSelected == true)
    {
        videoTargetPathTxt.Text = saveFileDialog.FileName;
    }
}