繁体   English   中英

具有来自XAML的参数控件类型的C#事件处理程序

[英]C# Eventhandler with parameter control type from xaml

我想调用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"/>

这就是我想要做的:

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

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

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

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

不,不是,您不需要,

在私有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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM