C#
- 2 ответа
- 0 вопросов
1
Вклад в тег
<ListBox ItemsSource="{Binding ItemCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding isChecked, Mode="TwoWay"}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Window x:Class="CheckBoxAll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CheckBoxAll"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<ListBox x:Name="ListItems" ItemsSource="{Binding ItemCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding isChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" local:CheckBoxExt.IsCheckExt="{Binding ElementName=CheckAll, Path=IsChecked}"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<CheckBox x:Name="CheckAll" Content="Select all"/>
</StackPanel>
</Grid>
</Window>
namespace CheckBoxAll
{
public class CheckBoxExt
{
public static bool GetIsCheckExt(DependencyObject obj)
{
return (bool)obj.GetValue(IsCheckExtProperty);
}
public static void SetIsCheckExt(DependencyObject obj, bool value)
{
obj.SetValue(IsCheckExtProperty, value);
}
// Using a DependencyProperty as the backing store for IsCheckExt. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsCheckExtProperty =
DependencyProperty.RegisterAttached("IsCheckExt", typeof(bool), typeof(CheckBoxExt), new PropertyMetadata((dp,arg) =>
{
if (dp is CheckBox)
{
var checkBox = dp as CheckBox;
checkBox.IsChecked = (bool)arg.NewValue;
}
}));
}
}