来自xaml的参数控制类型的C#Eventhandler

时间:2014-09-15 10:50:56

标签: c# wpf xaml eventhandler

我想调用onClick事件并从.xaml代码将Textbox对象传输到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"/>

这就是我想要做的事情:

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;
    }
}

2 个答案:

答案 0 :(得分:0)

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

保持事件处理程序正常,并用

替换代码
if (fileIsSelected == true)
{
    videoTargetPathTxt.Text = saveFileDialog.FileName;
}

答案 1 :(得分: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;
    }
}