0

我有一个包含允许用户添加和更改内容的ListView框。TextBoxes如何验证更改的内容与后面的 C# 中的任何现有内容不同?

xml:

<ListView 
   x:Name="_regionQueryListBox" 
   Width="122"  
   HorizontalAlignment="Left" 
   VerticalAlignment="Stretch"  
   DataContext="{Binding}" 
   IsSynchronizedWithCurrentItem="True" 
   Style="{StaticResource ListViewRegionSelectorStyle}" 
   ItemsSource="{Binding Path=Model}" 
   ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
   SelectionChanged="_regionQueryListBox_SelectionChanged">
   <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
      </Style>
   </ListView.ItemContainerStyle>
   <ListView.View>
      <GridView>
         <GridViewColumn Header="Region" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}">
            <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <TextBox 
                     HorizontalAlignment="Left" 
                     VerticalAlignment="Stretch" 
                     MaxLength="16"  
                     Width="110" 
                     Margin="-2,0,0,0" 
                     Padding="-2,0,0,0"
                     Text="{Binding Path=RegionName}"/>
               </DataTemplate>
            </GridViewColumn.CellTemplate>
         </GridViewColumn>
      </GridView>
   </ListView.View>
</ListView>
4

1 回答 1

0

是的,它是 MVVM。我有添加相同项目的验证,您可以找到如下模型:

private void OnQueryCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
        if (Model.Count == 0)
        {
            CurrentRegionViewModel = null;
        }
        if (args.Action == NotifyCollectionChangedAction.Add)
        {
            RegionQuery addedRegionQuery = args.NewItems.OfType<RegionQuery>().FirstOrDefault();
            if (addedRegionQuery != null)
            {
                string name = addedRegionQuery.RegionName;

                while (Model.Any(q => q.RegionName == name && q != addedRegionQuery))
                {
                    name += "*";
                }

                addedRegionQuery.RegionName = name;
            }
        }
于 2013-09-11T15:51:20.450 回答