gpt4 book ai didi

wpf - 在 DataTemplate 中使用 MVVM Light EventToCommand

转载 作者:行者123 更新时间:2023-12-04 05:07:37 28 4
gpt4 key购买 nike

所以我有一个 WPF 用户控件:

<UserControl x:Class="BI_Builder.Views.ObjectTreeView"
x:Name="UC1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:BI_Builder"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:viewModels="clr-namespace:BI_Builder.ViewModels"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding}">

<UserControl.Resources>
<ContentControl x:Key="Context" Content="{Binding}" />

<DataTemplate x:Key="DataSourceTemplate">
<TextBlock Text="{Binding Path=Name}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<command:EventToCommand Command="{Binding Path=DataContext.OpenCommand, Mode=OneWay,ElementName=UC1}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>

<HierarchicalDataTemplate x:Key="ItemTemplate"
ItemsSource="{Binding Children}"
ItemTemplate="{StaticResource DataSourceTemplate}">
<StackPanel>
<TextBlock Text="{Binding Header}">
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources>


<Grid>
<TreeView Name="TreeView" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource ItemTemplate}" >
</TreeView>
</Grid>
</UserControl>

这是用户控件的主视图模型:
public class ObjectTreeViewModel : ObservableObject       {

public ObservableCollection<ItemViewModel> Items {
get {
if (_items != null) return _items;
_items = new ObservableCollection<ItemViewModel>();
_items.Add(DataSources);
return _items;

}
set { _items = value;
}
}

public ItemViewModel DataSources {
get { return _dataSources ?? (_dataSources = new ItemViewModel() { Header = "Data Sources", Children = new ObservableCollection<object>(DataSourceList) }); }
set { _dataSources = value; }
}

public List<DataSource> DataSourceList;

public ICommand OpenCommand {
get { if (_openCommand == null) { return _openCommand = new RelayCommand(OpenDataSource); } return _openCommand; }

}

private void OpenDataSource() {
MessageBox.Show("Test");
}

public ObjectTreeViewModel() {
DataSourceList = new List<DataSource>();
DataSourceList.Add(new DataSource() { Name = "Test" });
}


private ItemViewModel _dataSources;
private ObservableCollection<ItemViewModel> _items;
private RelayCommand _openCommand;
}
}

我已经尝试了我在网络上遇到的所有方法来触发 DataSourceTemplate DataTemplate 中的 EventToCommand。事实上,我很确定它知道 OpenCommand 在哪里,因为如果我将路径更改为 gobbledygook,输出窗口会抛出一个错误,提示“ObjectTreeView”(这是绑定(bind)到UserControl) 没有 gobbledygook 属性。所以我想我已经正确设置了 DataContext ...

但是每当我点击文本 block 时......什么都没有。

真的试图避免代码隐藏(它只是感觉不对)和完全披露,我使用的是 MVVM Light 的 EventToCommand 但不是完整的工具包,尽管我很想重写我到目前为止的内容以查看是否使用服务定位器将解决这个问题。

最佳答案

TextBlock控制没有 Click事件 .见 MSDN .

您应该使用 MouseLeftButtonDown 事件代替:

<i:EventTrigger EventName="MouseLeftButtonDown">
<!-- ... -->
</i:EventTrigger>

关于wpf - 在 DataTemplate 中使用 MVVM Light EventToCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320280/

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