从WPF的上下文菜单中实现重命名功能的最佳方法是什么?我想要的是Windows资源管理器之类的功能,您可以右键单击某个项目,获得一个上下文菜单,如果选择重命名,则文本变为可编辑的。
到目前为止,我已经尝试了以下方法,但是我认为必须有更好的方法。这个解决方案的几乎任何方面都可以更改:)
<ListBox Grid.Row="0" MinWidth="200" MinHeight="75" ItemsSource="{Binding Path=DataSetsVM.DataSets, Source={StaticResource vmLocator}}" x:Name="dataSets">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="nameRoot">
<TextBlock x:Name="nameBlock" Text="{Binding Path=Name}" HorizontalAlignment="Stretch" MinWidth="200" Tag="{Binding DataContext, ElementName=dataSets}">
<TextBlock.ContextMenu>
<ContextMenu >
<ContextMenu.Resources>
<Style TargetType="Image">
<Setter Property="Height" Value="24"/>
<Setter Property="Width" Value="24"/>
</Style>
</ContextMenu.Resources>
<MenuItem Header="Rename" x:Name="RenameDatasetContext"
Command="{Binding PlacementTarget.Tag.CoreCommands.RenameDatasetCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ContextMenu}}}">
<MenuItem.Icon>
<Image Source="{StaticResource RenameLargeIcon}"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<TextBox x:Name="nameBox" Text="{Binding Path=Name}" HorizontalAlignment="Stretch" MinWidth="200" Visibility="Collapsed" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=IsEditable}" Value="True">
<Setter TargetName="nameBlock" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="nameBox" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
名称的设置者将执行以下操作:
set
{
if (IsEditable)
{
IsEditable = false;
if (_name == value)
{
RaisePropertyChanged(NamePropertyName);
return;
}
// Do some back end stuff
if (back end stuff worked)
{
var oldValue = _name;
_name = value;
RaisePropertyChanged(NamePropertyName);
}
else
{
// Do something about the error in the back end
}
}
}
该命令所做的所有操作均将IsEditable设置为true。
除非您没有真正更改名称,否则此方法将起作用,除非您没有更改名称,这时没有任何设置可以使IsEditable变回原来的名称,因为永远不会调用Name的 setter ,因此您可以在Rename模式下完成一堆项目。
我可以通过触发器在VM上设置属性,还是有更好的方法呢?
最佳答案
我简化了绑定(bind),以使样本简短易懂,我只是将ViewModel连接到了cs。后面的文件进行快速测试,所有编译/运行。
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525"
x:Name="wnd">
<ListBox ItemsSource="{Binding ViewModel.Items, ElementName=wnd}"
SelectedItem="{Binding ViewModel.SelectedItem, ElementName=wnd}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:Name="nameBlock" Text="{Binding Name}" Margin="4">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Rename"
Command="{Binding EditItemCommand}">
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<TextBox x:Name="nameBox" Text="{Binding Name}" Visibility="Collapsed" Margin="2,0"/>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter TargetName="nameBlock" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="nameBox" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后面的代码(请注意, View 完全是被动的):
public partial class MainWindow : Window
{
public ViewModel ViewModel { get; set; }
public MainWindow()
{
ViewModel = new ViewModel();
InitializeComponent();
}
}
viewModel(具有所有操作逻辑):
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<SomeClass> _items;
private SomeClass _selectedItem;
public ViewModel()
{
SelectedItem = Items.First();
}
public ObservableCollection<SomeClass> Items
{
get
{
if (_items == null)
{
_items = new ObservableCollection<SomeClass>();
for (int i = 0; i < 10; i++) _items.Add(new SomeClass(string.Format("name {0}", i)));
}
return _items;
}
}
public SomeClass SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem == value)
return;
_selectedItem = value;
foreach (var item in Items) item.IsEditable = false;
RaisePropertyChanged("SelectedItem");
}
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
具有完全不包含任何逻辑的属性的模型(对其他模型完全愚蠢,只知道如何设置自己的行为):
public class SomeClass : INotifyPropertyChanged
{
private string _name;
private bool _isEditable;
private DelegateCommand _editItemCmd;
public SomeClass(string name) { _name = name;}
public DelegateCommand EditItemCommand
{
get
{
return _editItemCmd ?? (_editItemCmd = new DelegateCommand(() => { IsEditable = true; }));
}
}
public string Name
{
get { return _name; }
set
{
if (_name == value)
return;
_name = value;
RaisePropertyChanged("Name");
}
}
public bool IsEditable
{
get { return _isEditable; }
set
{
if(_isEditable == value)
return;
_isEditable = value;
RaisePropertyChanged("IsEditable");
}
}
private void RaisePropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
关于c# - 从ContextMenu实现 “Rename”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9927092/