如何基于WPF中的属性值将样式应用于Listbox项
在上面的代码中,我需要基于属性值提供DataTrigger的值,而不是硬编码该值 提前感谢,,如何基于WPF中的属性值将样式应用于Listbox项,wpf,Wpf,在上面的代码中,我需要基于属性值提供DataTrigger的值,而不是硬编码该值 提前感谢,, Dinesh假设您的数据触发器的值基于属性CustomerNumber。然后添加另一个绑定属性,如下所示: <ListBox ItemsSource="{Binding Customers}"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="1" x:Name="Backgrou
Dinesh假设您的数据触发器的值基于属性CustomerNumber。然后添加另一个绑定属性,如下所示:
<ListBox ItemsSource="{Binding Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1" x:Name="BackgroundGrid">
<TextBlock Text="{Binding CustomerName}" />
<TextBlock Text="{Binding CustomerId}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding CustomerId}" Value="9-3453"><Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public bool CustomerIdMatchs
{
get{
return CustomerNumber==CustomerId;
}
}
然后绑定CustomerIDMatch而不是CustomerId,如下所示:
<ListBox ItemsSource="{Binding Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1" x:Name="BackgroundGrid">
<TextBlock Text="{Binding CustomerName}" />
<TextBlock Text="{Binding CustomerId}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding CustomerId}" Value="9-3453"><Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public bool CustomerIdMatchs
{
get{
return CustomerNumber==CustomerId;
}
}
假设DataTrigger的值基于属性CustomerNumber。然后添加另一个绑定属性,如下所示:
<ListBox ItemsSource="{Binding Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1" x:Name="BackgroundGrid">
<TextBlock Text="{Binding CustomerName}" />
<TextBlock Text="{Binding CustomerId}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding CustomerId}" Value="9-3453"><Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public bool CustomerIdMatchs
{
get{
return CustomerNumber==CustomerId;
}
}
然后绑定CustomerIDMatch而不是CustomerId,如下所示:
<ListBox ItemsSource="{Binding Customers}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="1" x:Name="BackgroundGrid">
<TextBlock Text="{Binding CustomerName}" />
<TextBlock Text="{Binding CustomerId}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding CustomerId}" Value="9-3453"><Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
public bool CustomerIdMatchs
{
get{
return CustomerNumber==CustomerId;
}
}
尝试使用带值转换器的多重绑定。您可以在这里找到一个示例:您需要在样式中设置背景,这是一个使用多重绑定和值转换器的优先问题。您可以在这里找到一个示例:您需要在样式中设置背景,这是一个优先问题