【发布时间】:2017-02-28 21:55:21
【问题描述】:
我遵循 MVVM 模式。我想将控件的属性值传递给同一控件的“CommandParameter”属性。但是遇到了“Object reference not set to an instance of object”的运行时异常。
WPF:
<Button
x:Name="btnBrowseFirmware1"
Grid.Row="2"
Grid.Column="1"
Width="135"
Height="35"
Command="{Binding OpenFileDialogCommand}"
CommandParameter="{Binding Name ,ElementName=btnBrowseFirmware1}"
Content="Browse "
Foreground="White"
/>
视图模型:
public class ConfigurationParametersViewModel : WorkspaceViewModelBase
{
public ICommand OpenFileDialogCommand { get; private set; }
public ConfigurationParametersViewModel()
: base("ConfigurationParameters", true)
{
OpenFileDialogCommand = new RelayCommand<string>(OpenFileDialogCommandFunc);
}
private void OpenFileDialogCommandFunc(string browseButtonName)
{
OpenFileDialog fileDialog = new OpenFileDialog();
Some Code...
}
}
【问题讨论】:
-
更改为 CommandParameter="{Binding Name ,RelativeSource={RelativeSource Self}}"
-
我运行了你的代码,它运行正常,这个异常到底发生在哪里?
-
我相信异常发生在 .NET 框架层。使用 snop 或查看 Visual Studio 绑定错误来理解它。如果您不介意,请将我的答案标记为您问题的正确答案。
-
@Rom:调试器没有在断点处中断,而是因运行时异常而崩溃。
标签: c# wpf mvvm data-binding