Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

wpf - DataTriggers in Expression Blend

I have a listbox that displays Shipment Items (custom class) that are formatted using a datatemplate (see below). There is a border element in the template (I am calling it a Gem) that displays which item is active (not selected) which will be the item to which products will be added.

template.png

There is a boolean property in the shipment class (Active) which is set in a click event handler for the Gem element. Normally the Gem is supposed to be dark as in the first image, then lit when active.

I have a DataTrigger set up in the DataTemplate to handle the change in the color of the Gem.

    <DataTemplate.Triggers>
 <DataTrigger Binding="{Binding Active}" Value="True">
  <Setter TargetName="ActiveGem" Property="Background">
   <Setter.Value>
    <RadialGradientBrush>
     <GradientStop Color="Red" Offset="0"/>
     <GradientStop Color="#FF820000" Offset="1"/>
    </RadialGradientBrush>
   </Setter.Value>
  </Setter>
 </DataTrigger>
 <DataTrigger Binding="{Binding Active}" Value="False">
  <Setter TargetName="ActiveGem" Property="Background">
   <Setter.Value>
    <RadialGradientBrush>
     <GradientStop Color="Red" Offset="0"/>
     <GradientStop Color="#FF820000" Offset="1"/>
    </RadialGradientBrush>
   </Setter.Value>
  </Setter> 
 </DataTrigger>
</DataTemplate.Triggers>

For some reason, however, when the application loads all of the gems are lit showing the packages are all active.

**Due to the reputation limits on stackOverflow I can't post more hyperlinks yet. Image below is at same domain as the one above.

/ExternalImages/ActiveItems.PNG

I have checked that all of the items are in fact Active = False, but the gem is always lit. Additionally, when I run the click event I go through the Package collection and set all Active = False then set the TemplatedParent of the sender to be active.

    Dim ActiveShipments = From ship In ShipmentData _
                         Where ship.Active = True
    For Each MyShipment As Shipment In ActiveShipments
        MyShipment.Active = False
    Next
    Dim Gem As Border = sender
    Dim ShipmentObject As ContentPresenter = Gem.TemplatedParent
    ShipmentObject.Content.Active = True

The Gems do not return to their unlit state (which they never were in the first place). They only remain lit. Any ideas?

Cory

--Oddly, Only showed up in the code box the first edit... Fixed now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, I figured it out. When I was making the RadialGradiantBrushes I either did not save the unlit one or I copy/pasted the lit on onto it and did not edit it or something. The datatrigger was firing properly, but since both gradients were of the lit variety, it looked like nothing was happening. Sorry for the trouble!

Cory


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...