0

我想调用 onClick 事件并将 Textbox 对象从 .xaml 代码传输到事件处理程序。那可能吗?

我会试着说明,

这是我的 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"/>

这就是我想要做的:

private void VideoTargetBtn_Click(object sender, RoutedEventArgs e, TextBox currTextbox)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    bool? fileIsSelected = saveFileDialog.ShowDialog();
    if (fileIsSelected == true)
    {
        currTextbox.Text = saveFileDialog.FileName;
    }
}
4

2 回答 2

0

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

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

if (fileIsSelected == true)
{
    videoTargetPathTxt.Text = saveFileDialog.FileName;
}
于 2014-09-15T11:02:08.220 回答
0

不,它不是,你不需要,

在私有 VideoTargetBtn_Click 处理程序中只需检索文本框内容

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;
    }
}
于 2014-09-15T11:04:39.620 回答