简体   繁体   English

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

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

I want to call onClick event and transfer Textbox object to the eventhandler from .xaml code. 我想调用onClick事件并将Textbox对象从.xaml代码转移到eventhandler。 is that posible? 那有可能吗?

i'll try to illustrate, 我会尽力说明

This is my Main.xaml.cs code: 这是我的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"/>

And that's what I want to do: 这就是我想要做的:

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

If all you need to do is access the control from your code behind you can just reference it by name since you use the x:Name attribute on the control with a value of videoTargetPathTxt. 如果您需要做的就是从后面的代码访问该控件,则可以按名称引用它,因为您在控件上使用了x:Name属性,其值为videoTargetPathTxt。

Leave the event handler as normal and replace your code with 保持事件处理程序正常,然后将代码替换为

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

No it isn't and you don't need to, 不,不是,您不需要,

in the private VideoTargetBtn_Click handler just retrieve the textbox contents 在私有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