2016-12-05 36 views
0

我是新來的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」觸發)

非常感謝

+0

使LstB_Item類實現INotifPropertyChanged(並在Checked屬性更改時觸發PropertyChanged事件)應該足夠。 – Clemens

+0

謝謝,你能解釋一下如何實現財產變更事件嗎? – GcH

回答

0

正如克萊門斯暗示類應實現INotifyPropertyChanged接口,提高每當經過屬性設置爲一個新的值變更通知:

public class LstB_Item : INotifyPropertyChanged 
    { 
     public string Title { get; set; } 

     private string _checked; 
     public string Checked 
     { 
      get { return _checked; } 
      set { _checked = value; NotifyPropertyChanged(); } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

然後,您應該能夠簡單地改變你想改變形象爲Checked屬性爲項的值:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    List<LstB_Item> items = LstB_Checklist.ItemsSource as List<LstB_Item>; 
    items[0].Checked = "true"; 
} 

請參閱MSDN有關INotifyPropertyChanged的更多信息接口:https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

你也應該考慮從字符串更改Checked屬性的類型爲BOOL:

 private bool _checked; 
     public bool Checked 
     { 
      get { return _checked; } 
      set { _checked = value; NotifyPropertyChanged(); } 
     } 
+0

非常感謝你們。它的工作原理和完全回答我的問題 – GcH