gpt4 book ai didi

c# - 使用 IConverter 处理 WPF/XAML/MVVM 中的 {NewItemPlaceholder}

转载 作者:太空狗 更新时间:2023-10-30 00:37:43 27 4
gpt4 key购买 nike

这是我的数据模板:

<UserControl.Resources>
<converter:PlaceholderConverter x:Key="_placeholderConverter"/>

<!-- Data(Display)Template for data objects of x:Type Customer-->
<DataTemplate DataType="{x:Type model:Customer}">
<!-- Customer Properties will be vertically stacked -->
<ContentControl >
<StackPanel>
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding Phone}"/>
</StackPanel>
</ContentControl>
</DataTemplate>
<UserControl.Resources>

还有两个不同的“容器”:

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Button Grid.Row="0"
Content="Delete"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="75"
Command="{Binding DeleteCommand}"/>

<DataGrid Grid.Row="1"
ItemsSource="{Binding Customers}"
SelectedItem="{Binding SelectedCustomer}"
AutoGenerateColumns="True"/>

<ListBox
Grid.Row="2"
ItemsSource="{Binding Customers, Mode=OneWay}"/>
</Grid>

和应用程序:

enter image description here

  1. 如何删除 {NewItemPlaceholder}? [完成,下面的解决方案]。
  2. 如何防止在单击上表中的一个空行以添加新行(我仍然可以添加行)时提到“{NewItemPlaceholder}”的绑定(bind)错误。

错误:

...Cannot convert '{NewItemPlaceholder}' from type 'NamedObject' to type 'CustomerExample.Model.Customer'...


...ConvertBack cannot convert value '{NewItemPlaceholder}' (type 'NamedObject'). BindingExpression:Path=SelectedCustomer; DataItem='CustomerViewModel'...

我可以编写 IConverter 实现,但如何将其绑定(bind)到 XAML 中?

提前致谢:-)

这是 IConverter 的实现:

public class PlaceholderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value.ToString() == "{NewItemPlaceholder}")
return DependencyProperty.UnsetValue;
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

为了绑定(bind)到单个项目,XAML 类似于:

<TextBlock Text="{Binding Name, Converter={StaticResource PlaceholderConverter}}"/>

但我认为我需要将其“全局”添加到数据收集元素中,而不是添加到绑定(bind)单个属性的位置。

最佳答案

您不需要绑定(bind)转换器。相反,将 ListBox 绑定(bind)到包装 Custumers 集合的 CollectionViewSource。 CollectionViewSource 跳过源集合中的 NewItemPlaceholder 元素。

<UserControl.Resources>
...
<CollectionViewSource x:Key="CustomersCVS" Source="{Binding Customers}"/>
</UserControl.Resources>

...
<ListBox ItemsSource="{Binding Source={StaticResource CustomersCVS}}"/>

您也不需要用于 SelectedItem 绑定(bind)的转换器。只需设置 Binding 的 TargetNullValue 属性:

<DataGrid SelectedItem="{Binding SelectedCustomer,
TargetNullValue={x:Static CollectionView.NewItemPlaceholder}}" .../>

关于c# - 使用 IConverter 处理 WPF/XAML/MVVM 中的 {NewItemPlaceholder},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902727/

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