原地址: http://www.cnblogs.com/yk250/p/5660340.html
效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现。这是项目中一个快捷键功能的扩展。
1,准备工作:VS2015 (15对WPF的支持变得异常的好,调试模式下允许自动更改属性。),随VS发布的Blend,几个基础类:
public class RelayCommand : ICommand
{
#region Fields readonly Action<object> _executeAct;
readonly Predicate<object> _canExecutePre;
private readonly Action _execute; private readonly Func<bool> _canExecute;
#endregion #region Constructors /// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
} /// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
} _executeAct = execute;
_canExecutePre = canExecute;
} /// <summary>
/// Initializes a new instance of the RelayCommand class that
/// can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
public RelayCommand(Action execute)
: this(execute, null)
{
} /// <summary>
/// Initializes a new instance of the RelayCommand class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
/// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
} _execute = execute;
_canExecute = canExecute;
} #endregion #region ICommand Members public bool CanExecute(object parameter)
{
if (parameter == null)
{
return _canExecute == null ? true : _canExecute();
} return _canExecutePre == null ? true : _canExecutePre(parameter);
} public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
} public void Execute(object parameter)
{
if (!CanExecute(parameter))
return;
if (parameter == null)
{
if (_execute != null)
_execute();
return;
}
if (_executeAct != null)
_executeAct(parameter); } #endregion
}
public class ModelBase : INotifyPropertyChanged
{
/// <summary>
/// 属性改变事件
/// </summary>
public event PropertyChangedEventHandler PropertyChanged; /// <summary>
/// 触发属性改变
/// </summary>
/// <param name="propertyName"></param>
protected virtual void RaisePropertyChangedEvent(string propertyName)
{
///一般写法
// PropertyChangedEventHandler handler = this.PropertyChanged;
// if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
///简易写法
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
2,前台XAML代码如下:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid>
<ItemsControl Grid.Row="1" Margin="15 30 30 20" x:Name="IT" VerticalAlignment="CENTER" HorizontalAlignment="center" ItemsSource="{Binding SettingListsSingle}" Visibility="visiBLE">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" ></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="AUTO"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Background="Brown" Tag="itgrid" x:Name="gr" Margin="0 0 5 0">
<Border BorderBrush="#FF00FF68" BorderThickness="0 0 0 2">
<TextBlock VerticalAlignment="Center" FontSize="16" Foreground="White" Text="{Binding Name}" TextBlock.TextAlignment="Center"></TextBlock>
</Border>
</Grid>
<ItemsControl x:Name="rowitems" Tag="{Binding GroupName}" HorizontalAlignment="Left" ItemsSource="{Binding TgLists}" Grid.Column="1" Margin="0 0 0 0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderBrush="#437DE5" BorderThickness="0 0 0 2" Padding="2">
<ToggleButton Visibility="Visible" Command="{Binding CheckedCommand}" Margin="0" FontSize="15" CommandParameter="{Binding DataContext.GroupName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}" IsChecked="{Binding IsChecked}" MaxWidth="200" Style="{DynamicResource ToggleButtonStyle}" >
<ToggleButton.Resources>
<local:ParmsConverter x:Key="pc"></local:ParmsConverter>
</ToggleButton.Resources>
<ToggleButton.Content>
<TextBlock x:Name="tbcontent" TextWrapping="Wrap" Text="{Binding Content}">
</TextBlock>
</ToggleButton.Content>
</ToggleButton>
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid> <ItemsControl Grid.Row="1" Margin="15 30 30 20" x:Name="IT2" VerticalAlignment="CENTER" HorizontalAlignment="CENTER" ItemsSource="{Binding SettingLists}" Visibility="visiBLE">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" ></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="AUTO"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Background="#437DE5" Tag="itgrid" x:Name="gr" Margin="0 0 5 0">
<Border BorderBrush="#FF00FF68" BorderThickness="0 0 0 2">
<TextBlock VerticalAlignment="Center" FontSize="16" Foreground="White" Text="{Binding Name}" TextBlock.TextAlignment="Center"></TextBlock>
</Border>
</Grid>
<ItemsControl x:Name="rowitems" Tag="{Binding GroupName}" HorizontalAlignment="Left" ItemsSource="{Binding TgLists}" Grid.Column="1" Margin="0 0 0 0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderBrush="#437DE5" BorderThickness="0 0 0 2" Padding="2">
<ToggleButton Visibility="Visible" Command="{Binding CheckedCommand}" Margin="0" FontSize="15" CommandParameter="{Binding DataContext.GroupName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}" IsChecked="{Binding IsChecked}" MaxWidth="120" Style="{DynamicResource ToggleButtonStyle}" >
<ToggleButton.Resources>
<local:ParmsConverter x:Key="pc"></local:ParmsConverter>
</ToggleButton.Resources>
<!--<ToggleButton.CommandParameter>
<MultiBinding Converter="{StaticResource pc}">
<Binding Path="."></Binding>
<Binding Path="DataContext.GroupName" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}"></Binding>
</MultiBinding>
</ToggleButton.CommandParameter>-->
<ToggleButton.Content>
<TextBlock x:Name="tbcontent" TextWrapping="Wrap" Text="{Binding Content}">
</TextBlock>
</ToggleButton.Content>
</ToggleButton> </Border>
<!--<Border BorderBrush="#437DE5" BorderThickness="0 0 0 2" Padding="2">
<RadioButton GroupName="{Binding Tag,ElementName=rowitems}" Checked="ToggleButton_Checked" Margin="0" FontSize="15" IsChecked="{Binding IsChecked}" MaxWidth="300" Content="{Binding Content}" Style="{DynamicResource ToggleButtonStyle}" />
</Border>-->
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
使用的主要样式如下:(一般用Blend完成,这里加入了2个进入和退场动画)
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<!--<Setter Property="MaxHeight" Value="></Setter>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ControlTemplate.Resources>
<SolidColorBrush x:Key="pathcolor" Color="#FF21A621"></SolidColorBrush>
<SolidColorBrush x:Key="buttonuncheckedcolor" Color="#FF767A76"></SolidColorBrush>
<SolidColorBrush x:Key="buttoncheckedcolor"></SolidColorBrush>
<SolidColorBrush x:Key="headercolor"></SolidColorBrush>
<Storyboard x:Key="Unchecked">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
<EasingColorKeyFrame KeyTime="0" Value="#FF21A621"/>
<EasingColorKeyFrame KeyTime="0:0:0.3" Value="sc#1, 0.114781961, 0.269301116, 0.114781961"/>
<EasingColorKeyFrame KeyTime="0:0:0.5" Value="Gray"/>
</ColorAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="br">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="30"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="br">
<EasingDoubleKeyFrame KeyTime="0" Value="20"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="br">
<EasingDoubleKeyFrame KeyTime="0" Value="20"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="br">
<EasingThicknessKeyFrame KeyTime="0" Value="5,2"/>
<EasingThicknessKeyFrame KeyTime="0:0:0.3" Value="0,2"/>
</ThicknessAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="checkBox">
<EasingDoubleKeyFrame KeyTime="0" Value="100"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="br">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="br">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Checked">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid">
<SplineColorKeyFrame KeyTime="0" Value="#FF767A76"/>
<SplineColorKeyFrame KeyTime="0:0:0.2" Value="sc#1, 0.114781961, 0.269301116, 0.114781961"/>
<SplineColorKeyFrame KeyTime="0:0:0.5" Value="#FF21A621"/>
</ColorAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="br">
<SplineDoubleKeyFrame KeyTime="0" Value="30"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="br">
<SplineDoubleKeyFrame KeyTime="0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="20"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="br">
<SplineDoubleKeyFrame KeyTime="0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="20"/>
</DoubleAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="br">
<SplineThicknessKeyFrame KeyTime="0" Value="0,2"/>
<SplineThicknessKeyFrame KeyTime="0:0:0.4" Value="5,2"/>
</ThicknessAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="checkBox">
<SplineDoubleKeyFrame KeyTime="0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="100"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="br">
<SplineDoubleKeyFrame KeyTime="0" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="br">
<SplineDoubleKeyFrame KeyTime="0" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid x:Name="grid" Height="AUTO" Width="AUTO" Background="{StaticResource buttonuncheckedcolor}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="AUTO"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Padding="5">
<Label TextElement.Foreground="WHITE" Content="{TemplateBinding Content}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="0 0 0 0" MaxWidth="{TemplateBinding MaxWidth}"/>
</Border>
<Border Grid.Column="1" Opacity="1" x:Name="br" VerticalAlignment="CENTER" HorizontalAlignment="CENTER" Margin="0 2" BorderThickness="2" Background="White" CornerRadius="15" RenderTransformOrigin="0.5,0.5" Height="0" Width="0" >
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0" ScaleY="0"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Path Margin="2" x:Name="checkBox" UseLayoutRounding="False" Stretch="Fill" Opacity="100" Fill="{StaticResource pathcolor}" Data="M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z"/>
</Border> </Grid>
<ControlTemplate.Triggers>
<!--<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource Unchecked_Copy2}"/>
</EventTrigger>-->
<Trigger Property="IsChecked" Value="true">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Checked}"></BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource Unchecked}"></BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<!--<EventTrigger RoutedEvent="ToggleButton.Unchecked">
<BeginStoryboard Storyboard="{StaticResource Unchecked}"/>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Checked">
<BeginStoryboard x:Name="Unchecked_Copy2_BeginStoryboard" Storyboard="{StaticResource Unchecked_Copy2}"/>
</EventTrigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
样式和颜色都是根据自己喜好来的,可以随意修改的。关键点事要用Path画出选中的钩,然后通过数据触发器将选中和不选中的姿态通过动画来展示:
<Trigger Property="IsChecked" Value="true">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Checked}"></BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource Unchecked}"></BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
3,定义ViewModel和相关结构:
首先是定义选中的3个状态:多行多选,多行单选,以及单行单选。(其它类型请自行添加和实现。)
public enum CheckType
{
MutilChecked,
SingleChecked,
RowChecked
}
然后是 设计层次结构:通用来讲我这里粗略的设计成嵌套类型,即数据源为List<setmodel>类型,而setmodel中又包含ObservableCollection<tgmodel>类型,而这个tgmodel呢,其实就是精确到的每一个按钮对应的模型了。下面给出具体定义:
public class setmodel : ModelBase
{
private string _name;
/// <summary>
/// 显示名称
/// </summary>
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChangedEvent("Name");
}
} private string _groupname = "singlecheck";
/// <summary>
/// 显示名称
/// </summary>
public string GroupName
{
get { return _groupname; }
set
{
_groupname = value;
RaisePropertyChangedEvent("GroupName");
}
}
public ObservableCollection<tgmodel> TgLists { get; set; } }
public class tgmodel : ModelBase
{
public tgmodel()
{
CommandDefine = new CommandDefine();
}
private string content;
/// <summary>
/// Toggle按钮显示内容
/// </summary>
public string Content
{
get { return content; }
set
{
content = value;
RaisePropertyChangedEvent("Content");
}
} private bool ischecked;
/// <summary>
/// 是否选中
/// </summary>
public bool IsChecked
{
get { return ischecked; }
set
{
ischecked = value;
RaisePropertyChangedEvent("IsChecked");
}
}
public ICommand CheckedCommand { get; set; }
public CommandDefine CommandDefine { get; set; }
}
然后这里写了一个队togglebutton 信息的辅助类:主要是封装了togglebutton对应的模型 TgModel,它所在组的 上一级模型 SetModel,所在行的名称 RowName。
public class TgInfo
{
public tgmodel TgModel { get; set; }
public setmodel SetModel { get; set; }
public string RowName { get; set; }
//public CheckType CheckType { get; set; }
//public List<setmodel> SourceLists { get; set; }
}
4,最后就是功能的实现了,这里代码有点乱,还有进一步修改的可能。
(1)初始化DataContext,先是ViewModel:
SettingListsSingle 是我上面单行拿来做选择项的集合,下面的三行数据数据源则是
SettingLists。
public class ViewModel : ModelBase
{
/// <summary>
/// 设置列表
/// </summary>
public List<setmodel> SettingLists { get; set; }
public List<setmodel> SettingListsSingle { get; set; } private CheckType _checktype;
/// <summary>
/// 选项类型
/// </summary>
public CheckType CheckType
{
get { return _checktype; }
set
{
_checktype = value;
RaisePropertyChangedEvent("CheckType");
}
} private CheckType _testchecktype;
/// <summary>
/// 选项类型
/// </summary>
public CheckType TestCheckType
{
get { return _testchecktype; }
set
{
_testchecktype = value;
RaisePropertyChangedEvent("TestCheckType");
}
} }
初始化ViewModel,后台代码:
DataContext = new ViewModel()
{
TestCheckType = CheckType.SingleChecked,
SettingListsSingle = new List<setmodel>() {
new setmodel() {
GroupName="GN0",
Name="选中类型",
TgLists =new ObservableCollection<tgmodel>()
{ new tgmodel() { Content="多选"},
new tgmodel() { Content="行单选"},
new tgmodel() { Content="全部单选" ,IsChecked=true },
}}, },
CheckType = CheckType.RowChecked,
SettingLists = new List<setmodel>() {
new setmodel() {
GroupName="GN1",
Name="测试数字",
TgLists =new ObservableCollection<tgmodel>()
{ new tgmodel() { Content="Test1"},
new tgmodel() { Content="Test2"},
new tgmodel() { Content="Test3"},
new tgmodel() { Content="Test4"},
new tgmodel() { Content="Test5",IsChecked=true},
}},
new setmodel() {
GroupName ="GN2",
Name="测试字母",
TgLists =new ObservableCollection<tgmodel>()
{ new tgmodel() { Content="TestA"},
new tgmodel() { Content="TestB"},
new tgmodel() { Content="TestC"},
new tgmodel() { Content="TestD"},
new tgmodel() { Content="TestE",IsChecked=true},
}},
new setmodel() {
GroupName="GN3",
Name="测试假名",
TgLists =new ObservableCollection<tgmodel>()
{ new tgmodel() { Content="Testあ"},
new tgmodel() { Content="Testい"},
new tgmodel() { Content="Testう"},
new tgmodel() { Content="Testえ"},
new tgmodel() { Content="Testお",IsChecked=true},
}},
}
};
最后是命令的初始化:
var Vm = (DataContext as ViewModel); Vm.SettingLists.ForEach(setmodel => setmodel.TgLists.ToList().ForEach(
(tgmodel=> tgmodel.CheckedCommand = new RelayCommand((pars) =>
{
TgInfo info = new TgInfo
{
RowName = pars.ToString(),
SetModel = setmodel,
TgModel = tgmodel
};
SetTgStatus(info, Vm,Vm.CheckType, Vm.SettingLists);
})))); Vm.SettingListsSingle.ForEach(setmodel => setmodel.TgLists.ToList().ForEach(
(tgmodel => tgmodel.CheckedCommand = new RelayCommand((pars) =>
{
TgInfo info = new TgInfo
{
RowName = pars.ToString(),
SetModel = setmodel,
TgModel = tgmodel
};
SetTgStatus(info, Vm, Vm.TestCheckType, Vm.SettingListsSingle);
}))));
设置命令实现如下:这里只是我的实现。
/// <summary>
/// 设置选中状态
/// </summary>
/// <param name="pars"></param>
/// <param name="Vm"></param>
private void SetTgStatus(params object[] parms)
{
var tginfo = parms[] as TgInfo;
var tgmodel = tginfo.TgModel;
var rowname = tginfo.RowName;
var setmodel = tginfo.SetModel;
var Vm = parms[] as ViewModel;
var checktype = (CheckType)parms[];
var settings = parms[] as List<setmodel>;
if (setmodel.Name=="选中类型")
{
var index = setmodel.TgLists.IndexOf(tgmodel);
Vm.CheckType = this[index];
}
if (checktype == CheckType.RowChecked)
{
settings.ForEach(sets =>
sets.TgLists.Where
(t => rowname == sets.GroupName&& t!=tgmodel).ToList().
ForEach(tg => tg.IsChecked = false));
}
else if (checktype == CheckType.SingleChecked)
{
settings.ForEach(sets =>
sets.TgLists.Where(t=>t!=tgmodel).ToList().
ForEach(tg => tg.IsChecked = false));
}
else
{ }
}
其中CheckType 用到了索引器,实现如下:
public CheckType this[int index]
{
get
{
return index == ? CheckType.MutilChecked :
(index == ? CheckType.RowChecked :
CheckType.SingleChecked);
} set
{
}
}
5,运行,OK。有什么问题可以一起讨论!
现在开始坚持每天一贴!也算是项目经验的总结和分享吧。当然肯定有很多不足之处,希望大家多多指教!