Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 更改WPF中的选定元素_C#_Wpf_Binding_Mvvm_Selection - Fatal编程技术网

C# 更改WPF中的选定元素

C# 更改WPF中的选定元素,c#,wpf,binding,mvvm,selection,C#,Wpf,Binding,Mvvm,Selection,我对WPF完全陌生 我正在用MVVM模式制作一个简单的应用程序 我有一个viewmodel,其中引用了一个模型。该模型包含一些我想放在组合框中的NetElement 以下是viewmodel的相关部分: public class MainWindowVM : ViewModelBase { private Model _model = null; public Model Model { get { return

我对WPF完全陌生

我正在用MVVM模式制作一个简单的应用程序

我有一个viewmodel,其中引用了一个模型。该模型包含一些我想放在组合框中的NetElement

以下是viewmodel的相关部分:

public class MainWindowVM : ViewModelBase
{
    private Model _model = null;

    public Model Model
    {
        get
        {
            return _model;
        }
    }

    #region ActiveElement

    private NetElement _activeElement = null;

    public NetElement ActiveElement
    {
        get
        {
            return _activeElement;
        }
        set
        {
            if (_activeElement != value)
            {
                _activeElement = value;
                RaisePropertyChanged("ActiveElement");
                if (ActiveElementChanged != null)
                    ActiveElementChanged(this, EventArgs.Empty);
            }
        }
    }
}
我希望能够在组合框中选择NetElement并将ActiveElement设置为它

以下是我当前XAML的相关部分:

<ItemsControl Background="White" IsTabStop="True" ItemsSource="{Binding Path=Model.RootNet.Elements}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Margin="2,6">
            <Hyperlink Command="{Binding Path=I'm not able to figure out what to write here}">
              <TextBlock Text="{Binding Path=Name}" />
            </Hyperlink>
          </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这不是一个组合框,而是一个文本块列表,但您可以看到它的去向


如何从视图中设置ActiveElement?

为组合框的SelectedItem属性创建与ActiveElement属性的绑定:

 <ComboBox SelectedItem="{Binding Path=ActiveElement}" ... />

然后将视图的DataContext属性设置为视图模型