wpf text mvvm multibinding

wpf - ¿Puedo hacer búsqueda de texto con multibinding



mvvm (2)

Desafortunadamente, TextSearch.Text no funciona en un DataTemplate. De lo contrario, podrías haber hecho algo como esto

<ComboBox ...> <ComboBox.ItemContainerStyle> <Style TargetType="{x:Type ComboBoxItem}"> <Setter Property="TextSearch.Text"> <Setter.Value> <MultiBinding StringFormat="{}{0}: {1}"> <Binding Path="BidServiceCategoryId"/> <Binding Path="BidServiceCategoryName"/> </MultiBinding> </Setter.Value> </Setter> </Style> </ComboBox.ItemContainerStyle> </ComboBox>

Sin embargo, esto no funcionará, entonces veo dos soluciones a su problema.

Primera forma
Establece IsTextSearchEnabled en True para ComboBox , reemplaza ToString en su clase de origen y cambia el MultiBinding en el TextBlock a un Binding

Xaml

<ComboBox ... IsTextSearchEnabled="True"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"/> </DataTemplate> </ComboBox.ItemTemplate>

Clase de fuente

public class TheNameOfYourSourceClass { public override string ToString() { return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName); } //... }

Second Way
Si no desea anular ToString, creo que deberá introducir una nueva propiedad en su clase de origen donde combine BidServiceCategoryId y BidServiceCategoryName para TextSearch.TextPath . En este ejemplo, lo llamo BidServiceCategory. Para que esto funcione, deberá llamar a OnPropertyChanged("BidServiceCategory"); cuando BidServiceCategoryId o BidServiceCategoryName cambian. Si son propiedades normales de CLR, puede hacer esto en el set , y si son propiedades de dependencia, tendrá que usar la propiedad de devolución de llamada cambiada

Xaml

<ComboBox ... TextSearch.TextPath="BidServiceCategory" IsTextSearchEnabled="True"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock DataContext="{Binding}"> <TextBlock.Text> <MultiBinding StringFormat="{}{0}: {1}"> <Binding Path="BidServiceCategoryId" /> <Binding Path="BidServiceCategoryName" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </ComboBox.ItemTemplate>

Clase de fuente

public class TheNameOfYourSourceClass { public string BidServiceCategory { get { return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName); } } private string m_bidServiceCategoryId; public string BidServiceCategoryId { get { return m_bidServiceCategoryId; } set { m_bidServiceCategoryId = value; OnPropertyChanged("BidServiceCategoryId"); OnPropertyChanged("BidServiceCategory"); } } private string m_bidServiceCategoryName; public string BidServiceCategoryName { get { return m_bidServiceCategoryName; } set { m_bidServiceCategoryName = value; OnPropertyChanged("BidServiceCategoryName"); OnPropertyChanged("BidServiceCategory"); } } }

Tengo el siguiente cuadro combinado en la aplicación mvvm-wpf. Necesito implementar "Búsqueda de texto" en este .. (junto con multibinding). Puede alguien ayudarme por favor.

<StackPanel Orientation="Horizontal"> <TextBlock Text="Bid Service Cat ID" Margin="2"></TextBlock> <ComboBox Width="200" Height="20" SelectedValuePath="BidServiceCategoryId" SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.SelectedBidServiceCategoryId.Value}" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.BenefitCategoryList}" Margin="12,0"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock DataContext="{Binding}"> <TextBlock.Text> <MultiBinding StringFormat="{}{0}: {1}"> <Binding Path="BidServiceCategoryId" /> <Binding Path="BidServiceCategoryName" /> </MultiBinding> </TextBlock.Text></TextBlock> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel>


No sé si su búsqueda de texto tiene que buscar TODO el texto, pero si desea buscar desde la ID de categoría, puede simplemente establecer la propiedad TextSearch.TextPath en BidServiceCategoryId. Eso también debería ser útil para cualquiera que desee utilizar la multiburación y descubra que la búsqueda de texto ya no funciona ... Funciona si establece explícitamente la propiedad TextPath.