将文本框绑定到 wpf 表单上的按钮

Binding a text box to a button on a wpf form

我有 3 个按钮和 3 个文本字段。我希望每个按钮都执行相同的方法,但是根据单击的按钮,应该填充一个文本不同的文本字段。我认为这可以通过将每个文本字段绑定到不同的按钮数据上下文来实现,但是该字段没有填充。这是我的 xaml -

<Window x:Class="vbaGitTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:vbaGitTool"
        mc:Ignorable="d"
        Title="Vba Git Tool" 
        Height="369" 
        Width="800" 
        MaxWidth="800" 
        MaxHeight="369"
        Background="Aquamarine">
    <Grid Margin="0,0,0,-1">
        <GroupBox Header="Create .txt File From Workbook" HorizontalAlignment="Left" Height="118" Margin="10,10,0,0" VerticalAlignment="Top" Width="772" BorderBrush="Black">
            <Grid Margin="0,0,0,4">
                <Button
                    x:Name="workbookSelect_btn"
                    Content="Select WB Path"
                    HorizontalAlignment="Left"
                    Margin="10,30,0,0"
                    VerticalAlignment="Top"
                    Width="102"
                    Click="Click_SelectFilePath" Height="23" />
                <TextBox 
                    x:Name="workbookPath_txt"
                    DataContext="{Binding 
                    ElementName=workbookSelect_btn,
                    Path=DataContext, 
                    Mode=OneWayToSource}"
                    HorizontalAlignment="Left" 
                    Height="23" 
                    Margin="126,30,0,0" 
                    TextWrapping="Wrap" 
                    VerticalAlignment="Top" 
                    Width="624"/>
            </Grid>
        </GroupBox>
        
        <GroupBox Header="Compare .txt Files" HorizontalAlignment="Left" Height="140" Margin="10,156,0,0" VerticalAlignment="Top" Width="772" BorderBrush="Black">
            <Grid Margin="0,0,0,-42">
                <Button 
                    x:Name="selectDev_btn"
                    Content="Select Dev File" 
                    HorizontalAlignment="Left" 
                    Margin="10,10,0,0" 
                    VerticalAlignment="Top" 
                    Width="96" 
                    Height="23"/>
                <Button 
                    x:Name="selectProd_btn"
                    Content="Select Prod File" 
                    HorizontalAlignment="Left" 
                    Margin="10,48,0,0" 
                    VerticalAlignment="Top" 
                    Width="96" 
                    Height="23"/>
                <TextBox 
                    x:Name="devFilePath" 
                    DataContext="{Binding 
                    ElementName=selectDev_btn,
                    Path=DataContext, 
                    Mode=OneWay}"
                    HorizontalAlignment="Left" 
                    Height="23" Margin="120,10,0,0" 
                    TextWrapping="Wrap" 
                    Text="" 
                    VerticalAlignment="Top" 
                    Width="630"/>
                <TextBox 
                    x:Name="prodFilePath" 
                    DataContext="{Binding 
                    ElementName=selectProd_btn,
                    Path=DataContext, 
                    Mode=OneWay}"
                    HorizontalAlignment="Left" 
                    Height="23" 
                    Margin="120,48,0,0" 
                    TextWrapping="Wrap" 
                    Text="" 
                    VerticalAlignment="Top" 
                    Width="630"/>
            </Grid>       
        </GroupBox>

    </Grid>
</Window>

这是可重复使用的方法

    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Click_SelectFilePath(object sender, RoutedEventArgs e)
        {

            System.Windows.Forms.OpenFileDialog ofd = 
                new System.Windows.Forms.OpenFileDialog();

            ofd.Title = "Find File";
            ofd.Filter = "Excel Files (*.xls) | *.xlsm | All Files(*.*) | *.*";  
            DialogResult dr = ofd.ShowDialog();

            this.DataContext = ofd.FileName;
        }
}

为了在 TextBox 或更好的 TextBlock 中设置文本,您可以在其 Text 属性 上设置绑定,数据从源流向目标,而不是 OneWayToSource:

<TextBlock Text="{Binding ElementName=workbookSelect_btn, Path=DataContext}"/>

<TextBlock Text="{Binding DataContext, ElementName=workbookSelect_btn}"/>

现在您还必须设置按钮的 DataContext,而不是 Window:

((Button)sender).DataContext = ofd.FileName;

为此目的使用 DataContext 看起来也很奇怪。考虑创建一个视图模型,Window 中的所有 UI 元素都将其属性绑定到该视图模型。在网上搜索 MVVM。

作为解决方法,请改用 Tag 属性:

<TextBlock Text="{Binding Tag, ElementName=workbookSelect_btn}"/>

((Button)sender).Tag = ofd.FileName;