2010-03-03 48 views
0

我在Silverlight中有一個列表框,裏面有一個項目列表。每個項目都有一定數量的附加選項,其可用性取決於每個項目。ListBox的OnDataItemBound()的Silverlight等效

<ListBox.ItemTemplate> 
     <DataTemplate> 
     <StackPanel Orientation="Horizontal">          
      <TextBlock Text="{Binding Name}" /> 
      <Button HorizontalAlignment="Right" x:Name="editDiarySecurityButton"> 
       <Image Source="/xxx.yyy.Silverlight.Common;Component/Resources/Images/Icons/Ribbon/Small/editSecurity.png" Width="16" Height="16" /> 
      </Button> 
     </StackPanel> 
     </DataTemplate>               
    </ListBox.ItemTemplate> 
</ListBox> 

的按鈕editDiarySecurityButton應根據該項目(表示日記)是否已經安全施加到其上,或者不進行處理。我可能會修改圖像的不透明度來反映這一點。

我的問題是我該如何做到這一點?在ASP.NET中,我會附加到ItemDataItemBound事件,但我不認爲這在WPF/Silverlight中可用。

回答

1

由於您特別詢問了不透明度屬性,將Button的不透明屬性綁定到您的DataContext上的一個返回double的屬性。將所需的任何邏輯添加到屬性中,以返回您希望該項目的不透明度。事情是這樣的:

<Button HorizontalAlignment="Right" x:Name="editDiarySecurityButton" 
Opacity="{Binding Path=ButtonOpacity, Mode=OneWay}"> 
    <Image Source="/xxx.yyy.Silverlight.Common;Component/Resources/Images/Icons/Ribbon/Small/editSecurity.png" Width="16" Height="16" /> 
</Button> 

然後在您的DataContext:

public double ButtonOpacity 
{ 
    get {return _buttonOpacity; } 
} 

使用與要控制按鈕的任何其他財產同樣的想法。

+0

謝謝。你知道,我還在進入這個WPF百靈。我從來沒有這樣想過。猜猜最好的辦法是對我的數據進行分類。雖然我的課程是來自EF/WCF的DTO,但我想我可能只需要包裝它。不幸的是,我只能選擇一個答案! – 2010-03-03 21:29:32

1

如果我明白你在問什麼,你會想要在對象上公開一個屬性並根據需要使用它來呈現你的元素。即要禁用按鈕,您可以使用IsEnabled =「{Binding HasSecurity}」之類的東西。