gpt4 book ai didi

c# - 绑定(bind)到 TreeView 选定项

转载 作者:行者123 更新时间:2023-12-03 10:38:06 30 4
gpt4 key购买 nike

我正在尝试将 TreeView 项目双击事件绑定(bind)到我的 View 模型。它实际上有效,只是我想知道选择了哪个项目,并且绑定(bind)到 SelectedItem 不起作用(参数为空):

<TreeView CommandBehaviors:MouseDoubleClick.Command="{Binding Connect}" CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}" 
Grid.Column="0" HorizontalAlignment="Stretch" DockPanel.Dock="Left" ItemsSource="{Binding Path=ServerItems, UpdateSourceTrigger=PropertyChanged}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Databases}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

命令行为:
public class MouseDoubleClick {
public static DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command",
typeof(ICommand),
typeof(MouseDoubleClick),
new UIPropertyMetadata(CommandChanged));

public static DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter",
typeof(object),
typeof(MouseDoubleClick),
new UIPropertyMetadata(null));

public static void SetCommand(DependencyObject target, ICommand value) {
target.SetValue(CommandProperty, value);
}

public static void SetCommandParameter(DependencyObject target, object value) {
target.SetValue(CommandParameterProperty, value);
}
public static object GetCommandParameter(DependencyObject target) {
return target.GetValue(CommandParameterProperty);
}

private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
Control control = target as Control;
if (control != null) {
if ((e.NewValue != null) && (e.OldValue == null)) {
control.MouseDoubleClick += OnMouseDoubleClick;
} else if ((e.NewValue == null) && (e.OldValue != null)) {
control.MouseDoubleClick -= OnMouseDoubleClick;
}
}
}

private static void OnMouseDoubleClick(object sender, RoutedEventArgs e) {
Control control = sender as Control;
ICommand command = (ICommand)control.GetValue(CommandProperty);
object commandParameter = control.GetValue(CommandParameterProperty);
command.Execute(commandParameter);
}
}

ViewModel 的相关部分:
public class MainViewModel : BaseViewModel {
#region commands
private ICommand _connect;

public ICommand Connect {
get {
if (_connect == null) {
_connect = new PGAdmin.Commands.Generic.RelayCommand(param => ConnectToDatabase(param));
}
return _connect;
}
set {
_connect = value;
}
}
#endregion

public void ConnectToDatabase(object param) {
DebugPopup.Show(param.ToString());
}
}

另一个问题 - 如果我完成这项工作 - 我会在 param 参数中得到什么 - 我的意思是 - 我可以以某种方式访问​​我的 observable 集合的底层项目吗?

最佳答案

你的参数绑定(bind)不正确CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}" .在这里你试图绑定(bind)到 SelectedItem您的 View 模型的属性,但该属性属于 TreeView .我认为您可以在 Visual Studio 输出窗口中看到相应的绑定(bind)错误。

试试这个代码:

CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}"

您可以找到有关 RelativeSource 的更多信息这里: RelativeSources in WPF

关于您的第二个问题 - 是的,您将从您的收藏中收到您的底层项目作为命令参数。

关于c# - 绑定(bind)到 TreeView 选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833013/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com