2016-05-19 31 views
1
<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> 

在上面的代碼中,我需要提供基於屬性值的DataTrigger值而不是硬編碼值。如何將樣式應用於列表框項目基於WPF中的屬性值

由於提前, 迪內希

+1

嘗試使用MultiBinding與值轉換器。你可以在這裏找到一個例子:https://social.msdn.microsoft.com/Forums/de-DE/729d2ac8-67db-4ab2-856b-8d56a9992bc5/datatrigger-binding-in-value?forum=wpf – MarkusE

+0

你需要在樣式中設置背景,這是一個優先問題 – nkoniishvt

回答

0

基於財產CUSTOMERNUMBER認爲你DataTrigger的價值。然後添加其他屬性綁定,就像這樣:

public bool CustomerIdMatchs 
{ 
    get{ 
     return CustomerNumber==CustomerId; 
    } 
} 

然後綁定CustomerIdMatchs代替客戶編號,像這樣:

<DataTrigger Binding="{Binding CustomerIdMatchs}" Value="true"> 
<Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/> 
</DataTrigger> 
相關問題