简体   繁体   English

访问XAML中绑定到文本框的相同数据

[英]Access the same data a textbox in the XAML is bound to

WPF MVVM in ViewModel I want to access the same data a textbox in the XAML is bound to 我想访问XAML中绑定到文本框的相同数据的ViewModel中的WPF MVVM

The XAML on MainWindow.xaml has a textbox bound to StoredProcs/ProcName MainWindow.xaml上的XAML具有绑定到StoredProcs / ProcName的文本框

<TextBox Name="txtProcName" Text="{Binding Path=StoredProcs/ProcName}"></TextBox>

And a Grid bound to StoredProcs Whenever the grid selection changes, the bound text in the textbox changes as it should. 绑定到StoredProcs的网格每当网格选择更改时,文本框中的绑定文本都会相应更改。

<DataGrid AutoGenerateColumns="False" 
    Height="300" Width="290"
    HorizontalAlignment="Center" 
    Name="dataGrid1" 
    VerticalAlignment="Top" 
    ItemsSource="{Binding StoredProcs}" 
    IsSynchronizedWithCurrentItem="True" 
    Margin="-6,0" Grid.RowSpan="2" Grid.Row="0">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Proc Name" Binding="{Binding ProcName}" >
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

I have a button that executes a procedure in MainWindowViewModel when clicked, that works 我有一个按钮,单击该按钮即可在MainWindowViewModel中执行一个过程

<Button Content="Create RDL" Command="{Binding CreateStoredProcedure}" />

In the CreateStoredProcedure code, I need to access the same data that is displayed in the textbox (not using code behind). 在CreateStoredProcedure代码中,我需要访问文本框中显示的相同数据(不使用后面的代码)。 I would think I need to get the StoredProcs/ProcName but can't figure out how to do that. 我认为我需要获取StoredProcs / ProcName,但无法弄清楚该怎么做。

I tried adding CommandParameter to the XAML but don't know how to access it in the CreateStoredProcedure instructions as it won't allow me to add paramaters 我尝试将CommandParameter添加到XAML,但是在CreateStoredProcedure指令中不知道如何访问它,因为它不允许我添加参数

void CreateStoredProcedureExecute()
{
    string procName = "proc";
    //procName = { StoredProcs/ProcName };
    MessageBoxResult result = 
        MessageBox.Show(String.Format("Create Stored Procedure {0}", procName));
}

bool CanCreateStoredProcedure()
{
    return true;
}

public ICommand CreateStoredProcedure 
{ 
    get 
    { 
        return new RelayCommand(CreateStoredProcedureExecute, 
            CanCreateStoredProcedure); 
    } 
}

Unless I am misunderstanding your question, you should be able to get the value of the property that the TextBox is bound to from within CreateStoredProcedure. 除非我对您的问题有误解,否则您应该能够从CreateStoredProcedure中获取TextBox绑定到的属性的值。

One thing though, if you want the TextBox to update the property, make sure you add "Mode=TwoWay" to your binding expression: 但是,有一件事,如果您希望TextBox更新属性,请确保在绑定表达式中添加“ Mode = TwoWay”:

<TextBox Name="txtProcName" Text="{Binding Path=StoredProcs/ProcName, Mode=TwoWay}"></TextBox>

Unless I misunderstood I think you want something like this? 除非我误解了,否则我想你想要这样的东西吗?

<Button 
  Content="Create RDL" 
  Command="{Binding CreateStoredProcedure}" 
  CommandParameter="{Binding ElementName=txtProcName, Path=Text}"/>

However as the other answer states, you should be able to just access the property in the ViewModel that is backing the textbox from the command, but if for some reason you cannot my code should work as well. 但是,正如其他答案所指出的那样,您应该只能访问从命令支持文本框的ViewModel中的属性,但是如果由于某些原因您不能使我的代码正常工作。

(Assuming you are defining RelayCommand as defined by this MSDN article, this should fix your other problem) (假设你定义所界定RelayCommand 这个 MSDN文章,这应该解决您的其他问题)

public ICommand CreateStoredProcedure 
{ 
    get 
    { 
        return new RelayCommand<object>(
            (object parameter) => CreateStoredProcedureExecute(parameter), 
            (object parameter) => CanCreateStoredProcedure); 
    } 
}


private void CreateStoredProcedureExecute(object parameter)
{
    string ProcName = parameter as string;
}

I will admit my somewhat inexperience with setting up commands like this, but I did find a working example in my code that followed this, so hopefully it helps. 我会承认我对设置这样的命令有些经验,但是我确实在代码中找到了一个有效的示例,因此希望对它有所帮助。

I think KDiTraglia has the right solution. 我认为KDiTraglia有正确的解决方案。 The only thing I would do differently is to bind the CommandParameter to the model, not the UI element. 我要做的唯一不同的事情是将CommandParameter绑定到模型,而不是UI元素。

<Button 
     Content="Create RDL"
     Command="{Binding CreateStoredProcedure}" 
     CommandParameter="{Binding Path=StoredProcs/ProcName}" />

I'm assuming that StoredProcs/ProcName is a placeholder for a real, valid binding path. 我假设StoredProcs/ProcName是真实有效绑定路径的占位符。

Root around here for more information: http://msdn.microsoft.com/en-us/library/ms752308 在此处深入了解更多信息: http : //msdn.microsoft.com/zh-cn/library/ms752308

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

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