暂时还有一些bug,基本上还有问题,等后续更新
主要代码:
public class SymbolEdit : TextBox, INotifyPropertyChanged
{
public SymbolEdit()
: base()
{
DataCollection = new ObservableCollection<BookViewModel>()
{
new BookViewModel(){Name = "C语言程序设计",Price =39.9,Publisher = "谭浩强"},
new BookViewModel(){Name = "WPF深入浅出",Price =39.9,Publisher = "谭浩强"}
};
DataCollectionView = CollectionViewSource.GetDefaultView(DataCollection);
DataCollectionView.Filter = new Predicate<object>(OnFilter);
this.PropertyChanged += SymbolEdit_PropertyChanged;
}
private void SymbolEdit_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("SelctedInstance"))
{
if (SelctedInstance != null)
{
InputValue = SelctedInstance.Name;
App.Current.Dispatcher.Invoke(()=>DataCollectionView.Refresh());
}
}
if(e.PropertyName.Equals("InputValue"))
{
App.Current.Dispatcher.Invoke(()=>DataCollectionView.Refresh());
}
}
private bool OnFilter(object mod)
{
var book = mod as BookViewModel;
if (book != null && !string.IsNullOrWhiteSpace(InputValue))
{
return book.Name.Contains(InputValue) || book.Name.Equals(InputValue, StringComparison.CurrentCultureIgnoreCase);
}
else
return true;
}
private static ICollectionView dataCollectionView;
private ObservableCollection<BookViewModel> dataCollection;
public static ICollectionView DataCollectionView
{
get { return dataCollectionView; }
set
{
if (dataCollectionView != value)
{
dataCollectionView = value;
//OnPropertyChanged("DataCollectionView");
};
}
}
public ObservableCollection<BookViewModel> DataCollection
{
get { return dataCollection; }
set
{
if (dataCollection != value)
{
dataCollection = value;
OnPropertyChanged("DataCollection");
};
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register("InputValue",
typeof(string), typeof(SymbolEdit), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure,
new PropertyChangedCallback(TotalPropertyChanged)));
public string InputValue
{
get { return (string) GetValue(InputValueProperty); }
set { SetValue(InputValueProperty,value); }
}
private static void TotalPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((SymbolEdit)d).OnPropertyChanged(e.Property.Name);
}
private BookViewModel selctedInstance;
public BookViewModel SelctedInstance
{
get { return selctedInstance; }
set
{
if (selctedInstance != value)
{
selctedInstance = value;
OnPropertyChanged("SelctedInstance");
}
}
}
}
样式
<Style TargetType="{x:Type controls:SymbolEdit}">
<!--<Setter Property="Width" Value="200"/>-->
<Setter Property="Height" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:SymbolEdit}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" >
<TextBox Focusable="True" Width="Auto" MinWidth="200" x:Name="txt_Input" Text="{Binding InputValue,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
</TextBox>
</StackPanel>
<!--数据匹配列表-->
<Popup x:Name="dataList" StaysOpen="True" PlacementTarget="{Binding ElementName=txt_Input}" AllowsTransparency="True" >
<ListView x:Name="_listView" Focusable="True" AllowDrop="True" IsEnabled="True" Width="Auto" Height="Auto"
SelectedValue="{Binding SelctedInstance,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
ItemsSource="{Binding DataCollectionView,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
>
<ListView.View>
<GridView AllowsColumnReorder="True">
<!--<GridViewColumn Header="选择" CellTemplate="{StaticResource checkBoxGridViewColumn}"/>-->
<GridViewColumn Header="书名" Width="Auto" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="价格" Width="Auto" DisplayMemberBinding="{Binding Price}"/>
<GridViewColumn Header="作者" Width="Auto" DisplayMemberBinding="{Binding Publisher}"/>
</GridView>
</ListView.View>
</ListView>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" SourceName="txt_Input" Value="True">
<Setter TargetName="dataList" Property="IsOpen" Value="True"/>
</Trigger>
<Trigger Property="Text" SourceName="txt_Input" Value="">
<Setter TargetName="dataList" Property="IsOpen" Value="False"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" SourceName="txt_Input" Value="False"/>
<Condition Property="IsFocused" SourceName="dataList" Value="False"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="dataList" Property="IsOpen" Value="False"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" SourceName="txt_Input" Value="False"/>
<Condition Property="IsFocused" SourceName="dataList" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="dataList" Property="IsOpen" Value="True"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>