Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击按钮时检索文本框值失败_C#_.net_Wpf_Binding_Textbox - Fatal编程技术网

C# 单击按钮时检索文本框值失败

C# 单击按钮时检索文本框值失败,c#,.net,wpf,binding,textbox,C#,.net,Wpf,Binding,Textbox,看看标题,它可能看起来很简单,但它的小技巧。我正在开发一个wpf应用程序,我需要动态生成按钮、标签和文本框。在我的VoltageView XAml文件中,我创建了一个stackpanel。在我的VoltageChannelView xaml文件中,我创建了所有UI组件 我在一定程度上实现了以下目标: VoltageViewModel mVoltageViewModel = new VoltageViewModel(); // Called in constructor public void

看看标题,它可能看起来很简单,但它的小技巧。我正在开发一个wpf应用程序,我需要动态生成按钮、标签和文本框。在我的VoltageView XAml文件中,我创建了一个stackpanel。在我的VoltageChannelView xaml文件中,我创建了所有UI组件

我在一定程度上实现了以下目标:

VoltageViewModel mVoltageViewModel = new VoltageViewModel();

// Called in constructor
public void OnChildAdd()
    {            
        foreach (VoltageBoardChannel mVoltageChannelViewModel in mVoltageViewModel.VoltageChannelList)
        {
            VoltageChannelView mVoltageChannelView = new VoltageChannelView();
            mVoltageChannelView.Margin = new Thickness(2);
            mVoltageChannelView.ChannelInfo = mVoltageChannelViewModel;
            stackPanel.Children.Add(mVoltageChannelView);
        }
    }
电压视图:

<Grid Grid.Row="1" Style="{DynamicResource styleBackground}" Name="VoltageChannels" >
        <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"></StackPanel>
</Grid>
<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />
private string mChannelName;
    public string ChannelName
    {
        get
        {
            return mChannelName;
        }
        set
        {
            mChannelName = value;
            OnPropertyChanged("ChannelName");
        }
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get; set;
    }

    string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }      
public void DoSomethingExecute(object param)
{
    // param is your string        
    //Do Something with VoltageText
}
电压视图模型类:

public ObservableCollection<VoltageBoardChannel> channelList = null;

    public ObservableCollection<VoltageBoardChannel> redhookChannels = new ObservableCollection<VoltageBoardChannel>
    {             
         new VoltageBoardChannel { ChannelName = "VDD_IO_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_CODEC_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_DAL_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_DPD_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_PLL_AUD", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD_AMP1_AUD", IsAvailable = true}             
    };       

    public ObservableCollection<VoltageBoardChannel> bavaria1Channels = new ObservableCollection<VoltageBoardChannel>
    {
         new VoltageBoardChannel { ChannelName = "VDD__MAIN", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__IO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__CODEC", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__LDO", IsAvailable = true},
         new VoltageBoardChannel { ChannelName = "VDD__AMP", IsAvailable = true},  

    };        

    public VoltageViewModel()
    {
        channelList = new ObservableCollection<VoltageBoardChannel>();
        channelList = bavaria1Channels;          

    }

    public ObservableCollection<VoltageBoardChannel> VoltageChannelList
    {
        get 
        { 
            return channelList; 
        }

        set
        { 
            channelList = value;
            OnPropertyChanged("ChannelList");
        }
    }

    RelayCommand _voltageCommand;
    public ICommand VoltageCommand
    {
        get
        {
            if (_voltageCommand == null)
            {
                _voltageCommand = new RelayCommand(param => this.DoSomethingExecute, param => this.DoSomethingCanExecute);
            }
            return _voltageCommand;
        }
    }

    public bool DoSomethingCanExecute(object param)
    {
        return true;
    }

    public void DoSomethingExecute(object param)
    {

    }
因此,当我运行应用程序时,它会动态显示列表中维护的巴伐利亚1频道4倍。现在,在每个动态生成的控件上都有一个文本框和按钮

您可以注意到,在
VoltageChannelView.xaml中,我绑定了bw按钮和文本框。我想在文本框中输入值,然后单击设置按钮,它将检索输入的值,以便进行进一步的操作。基本上应该有一个设置按钮的事件,它应该传递文本框中写入的文本作为参数


我怎样才能实现它?:)

当我看到这一点时,您是否缺少命令的实现。 您需要的是处理命令的逻辑

复制本文的源代码

RelayCommand类

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
    : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;           
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}
执行方法:

<Grid Grid.Row="1" Style="{DynamicResource styleBackground}" Name="VoltageChannels" >
        <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"></StackPanel>
</Grid>
<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />
private string mChannelName;
    public string ChannelName
    {
        get
        {
            return mChannelName;
        }
        set
        {
            mChannelName = value;
            OnPropertyChanged("ChannelName");
        }
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get; set;
    }

    string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }      
public void DoSomethingExecute(object param)
{
    // param is your string        
    //Do Something with VoltageText
}
CanExecutemethod

public bool DoSomethingCanExecute(object param)
{
    return true;        
}

当我看到这一点时,您是否缺少您的命令的实现。 您需要的是处理命令的逻辑

复制本文的源代码

RelayCommand类

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;        

    #endregion // Fields

    #region Constructors

    public RelayCommand(Action<object> execute)
    : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;           
    }
    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion // ICommand Members
}
执行方法:

<Grid Grid.Row="1" Style="{DynamicResource styleBackground}" Name="VoltageChannels" >
        <StackPanel Height="Auto" Name="stackPanel" Width="Auto" MinHeight="300"></StackPanel>
</Grid>
<Label Grid.Column="0" Content="{Binding ChannelName}" />
<TextBox Grid.Column="1" Text="{Binding VoltageText}" />
<Button Grid.Column="1" Content="Set" CommandParameter="{Binding VoltageText}" Command="{Binding VoltageCommand}" />
private string mChannelName;
    public string ChannelName
    {
        get
        {
            return mChannelName;
        }
        set
        {
            mChannelName = value;
            OnPropertyChanged("ChannelName");
        }
    }

    private bool mIsAvailable;
    public bool IsAvailable
    {
        get; set;
    }

    string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }      
public void DoSomethingExecute(object param)
{
    // param is your string        
    //Do Something with VoltageText
}
CanExecutemethod

public bool DoSomethingCanExecute(object param)
{
    return true;        
}


您在哪里设置VoltageCommand?@Mark:存在于模型类:)是否缺少代码只有公共ICommand VoltageCommand{get;set;}?@Mark:是的只有
公共ICommand VoltageCommand{get;set;}
。这是他们的问题吗?你能详细说明一下你在哪里设置voltage命令吗?@Mark:模型类中有:)代码丢失了吗?只有公共ICommand voltage命令{get;set;}?@Mark:是的,只有
公共ICommand voltage命令{get;set;}
。这是他们的问题吗?我从模型类中删除了VoltageCommand,并添加了上面编写的代码。它抛出错误
只有赋值、调用、递增、递减和新对象表达式可以用作this.DoSomethingExecute和this.DoSomethingCanExecute的语句。另外,我将如何获得文本框中输入的文本?您还需要一个DoSomethingCanExecute方法如果您需要它,我将更新我的答案。您在DoSomethingExecute(object param)中获得的voltage命令文本该参数是您的文本。可以这样做:
new VoltageBoardChannel{ChannelName=“VDD_umain”,IsAvailable=true,voltage命令=m_voltage命令}
使用此m_voltage命令,制作一个delegatecommand并给我通过某种方法输入的文本???是的,我添加了两种方法,但仍然在
处抛出错误。doSomethingeExecute
您的方法与上面的示例类似吗?我已经检查了它应该工作的代码。我从模型类中删除了VoltageCommand,并添加了您上面编写的代码。它抛出错误
只有赋值、调用、递增、递减和新对象表达式可以用作this.DoSomethingExecute和this.DoSomethingCanExecute的语句。另外,我将如何获得文本框中输入的文本?您还需要一个DoSomethingCanExecute方法如果您需要它,我将更新我的答案。您在DoSomethingExecute(object param)中获得的voltage命令文本该参数是您的文本。可以这样做:
new VoltageBoardChannel{ChannelName=“VDD_umain”,IsAvailable=true,voltage命令=m_voltage命令}
使用此m_voltage命令,制作一个delegatecommand并给我通过某种方法输入的文本???是的,我添加了两种方法,但仍然在
处抛出错误。doSomethingeExecute
您的方法与上面的示例类似吗?我已经检查了代码,它应该可以工作了。