这个还有一些问题,需要花点时间去整理一下
<UserControl x:Class="WPFMVVM.Controls.CommodityEdit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMVVM.Controls">
<UserControl.Template>
<ControlTemplate TargetType="{x:Type local:CommodityEdit}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBox x:Name="txt_Input" Focusable="True" Width="Auto" MinWidth="200" 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,UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding DataCollectionView,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"
>
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn Header="书名" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="价格" DisplayMemberBinding="{Binding Price}"/>
<GridViewColumn Header="作者" DisplayMemberBinding="{Binding Publisher}"/>
</GridView>
</ListView.View>
</ListView>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="txt_Input" Property="IsFocused" Value="True">
<Setter TargetName="dataList" Property="IsOpen" Value="True"></Setter>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition SourceName="txt_Input" Property="IsFocused" Value="False"></Condition>
<Condition SourceName="dataList" Property="IsFocused" Value="False"></Condition>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="dataList" Property="IsOpen" Value="False"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition SourceName="txt_Input" Property="IsFocused" Value="False"></Condition>
<Condition SourceName="dataList" Property="IsFocused" Value="True"></Condition>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter TargetName="dataList" Property="IsOpen" Value="True"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Template>
</UserControl>
C#代码
public partial class CommodityEdit:UserControl, INotifyPropertyChanged
{
//public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register(
// "InputValue", typeof(string), typeof(CommodityEdit), new PropertyMetadata(string.Empty, OnContentPropertyChanged),
// OnValidateCallBack);
//private static void OnContentPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
//{
//}
//private static bool OnValidateCallBack(object value)
//{
// return true;
//}
//public string InputValue
//{
// get { return (string)GetValue(ContentProperty); }
// set { SetValue(ContentProperty, value); }
//}
public CommodityEdit()
{
InitializeComponent();
DataCollection = new ObservableCollection<BookViewModel>()
{
new BookViewModel(){Name = "C语言程序设计",Price =39.9,Publisher = "谭浩强"}
};
DataCollectionView = CollectionViewSource.GetDefaultView(DataCollection);
DataCollectionView.Filter = new Predicate<object>(OnFilter);
this.PropertyChanged += CommodityEdit_PropertyChanged;
}
void CommodityEdit_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("SelctedInstance"))
{
if (SelctedInstance != null)
{
InputValue = SelctedInstance.Name;
DataCollectionView.Refresh();
}
}
}
private string inputValue;
public string InputValue
{
get { return inputValue; }
set
{
if (inputValue != value)
{
inputValue = value;
OnPropertyChanged("InputValue");
DataCollectionView.Refresh();
}
}
}
private bool OnFilter(object mod)
{
//Console.WriteLine(DataCollectionView.Count);
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 ICollectionView dataCollectionView;
private ObservableCollection<BookViewModel> dataCollection;
public 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");
};
}
}
private BookViewModel selctedInstance;
public BookViewModel SelctedInstance
{
get { return selctedInstance; }
set
{
if (selctedInstance != value)
{
selctedInstance = value;
OnPropertyChanged("SelctedInstance");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}