我是新來的WPF,我正在探索listbox控件。 我創建了一個列表框,項目代表圖像加文本。WPF列表框中的圖像,如何dynamycally更改圖像的條件
XAML代碼:
<ListBox x:Name="LstB_Checklist" HorizontalAlignment="Left" Height="139" Margin="48,61,0,0" VerticalAlignment="Top" Width="220">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Checked}" Value="false">
<Setter Property="Source" Value="pack://application:,,,/listbox;component/Pictures/BulletOff.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Checked}" Value="true">
<Setter Property="Source" Value="pack://application:,,,/listbox;component/Pictures/BulletOn.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
的結合允許在啓動時正確設置項目和形象。
代碼:
public MainWindow()
{
InitializeComponent();
List<LstB_Item> items = new List<LstB_Item>();
items.Add(new LstB_Item() { Title = "Item1", Checked = "false" });
items.Add(new LstB_Item() { Title = "Item2", Checked = "false" });
LstB_Checklist.ItemsSource = items;
}
public class LstB_Item
{
public string Title { get; set; }
public string Checked { get; set; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//
}
我想知道如何根據某些條件下改變形象,當我在一個按鈕,短聲,根據(如所選項目的圖像轉向「bulletOn」而不是「bulltOff」外部條件,而不是基於「ONSELECT」觸發)
非常感謝
使LstB_Item類實現INotifPropertyChanged(並在Checked屬性更改時觸發PropertyChanged事件)應該足夠。 – Clemens
謝謝,你能解釋一下如何實現財產變更事件嗎? – GcH