[英]WPF ListBox with CheckBox data template - Binding Content property of Checkbox not working
[英]Property binding not working in ListBox WPF
我試圖用包含一個復選框和一個文本塊的網格組成的項目填充一個ListBox。 我最初只有一個復選框,但是我需要包裝文本,因此我將其更改為帶有復選框和文本塊的網格,文本塊進行了文本包裝。
當只是復選框時,綁定起作用,但現在不起作用。 現在,正確數量的項目顯示在列表框中,但這只是一個未選中的復選框(即使我將其設置為在后面的代碼中選中,請參見下文)和一個空白的文本塊。
XAML:
<ListBox
x:Name="lstCalibration"
Margin="304,95,78,24"
Background="#7FFFFFFF"
Width="211"
HorizontalAlignment="Center"
VerticalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="auto">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox
Content=""
Tag="{Binding Tag}"
IsChecked="{Binding IsChecked}"
Checked="CheckBoxCal_Checked"
Unchecked="CheckBoxCal_Unchecked"
Grid.Row="0" Grid.Column="0"
/>
<TextBlock
Tag="{Binding Tag}"
Text="{Binding Text}"
TextWrapping="Wrap"
Grid.Row="0"
Grid.Column="1"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
后面的代碼:
foreach (Controller item in calList)
{
//fill listbox with cals and correct checked states
Grid grid = new Grid();
CheckBox cb = new CheckBox();
TextBlock tb = new TextBlock();
tb.Text = item.Description;
cb.Tag = tb.Tag = i;
cb.IsChecked = calChecked[i];
if (cb.IsChecked == true)
noneChecked = false;
i++;
Grid.SetRow(cb, 0);
Grid.SetColumn(cb, 0);
grid.Children.Add(cb);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 1);
grid.Children.Add(tb);
lstCalibration.Items.Add(grid);
}
添加它,然后刪除該foreach循環,在該循環中以編程方式創建所有已在DataTemplate
設置的內容。 這是一個非常好的DataTemplate
,但是您的代碼沒有正確使用它。
擺脫
calChecked
;
_items[n].IsChecked
現在將完成該工作。
您可以隨時將新的ListItem
添加到_items
並將其顯示在列表中,或刪除現有的ListItem
並將其從列表中消失。 那是“可觀察的”部分。 由於引發了PropertyChanged
事件,如果您從后面的代碼中對其中任何一個設置了IsChecked
,則UI中的相應復選框將自動更新。
public class ListItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public String Text { get; set; }
private bool _isChecked = false;
public bool IsChecked {
get { return _isChecked; }
set {
_isChecked = value;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(IsChecked));
}
}
}
在您的構造函數中的代碼后面:
private ObservableCollection<ListItem> _items;
public MyWindow()
{
InitializeComponent()
_items = new ObservableCollection<ListItem>(
calList.Select(
cal => new ListItem {
Text = cal.Description,
IsChecked = cal.WhateverElse
}
));
lstCalibration.Items = _items;
}
在以下兩個地方對DataTemplate
進行了一個小的更改:不要浪費時間綁定Tag
。 在事件處理程序中(您甚至可能不需要),只需像這樣進行轉換:
public void CheckBoxCal_Checked(object sender, EventArgs args)
{
var listItem = ((FrameworkElement)sender).DataContext as ListItem;
// Do whatever. listItem.IsChecked will be up to date thanks
// to the binding.
}
XAML:
<CheckBox
Content=""
IsChecked="{Binding IsChecked}"
Checked="CheckBoxCal_Checked"
Unchecked="CheckBoxCal_Unchecked"
Grid.Row="0" Grid.Column="0"
/>
<TextBlock
Text="{Binding Text}"
TextWrapping="Wrap"
Grid.Row="0"
Grid.Column="1"
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.