MVVM下的WPF自定义控件自定义控件要实现的功能为:
1:形状为圆形,圆形的填充色可以绑定Brush。
2:控件有Command和CommandParameter属性。两者都可以绑定。
3:Command绑定的事件由双击控件来触发。
希望各位大侠些给提供一个demo版本的程序吧,我在网上找了很多资料,但都不太合适。请大家帮帮忙啊~WPFMVVM自定义控件command双击事件
1:形状为圆形,圆形的填充色可以绑定Brush。
2:控件有Command和CommandParameter属性。两者都可以绑定。
3:Command绑定的事件由双击控件来触发。
希望各位大侠些给提供一个demo版本的程序吧,我在网上找了很多资料,但都不太合适。请大家帮帮忙啊~WPFMVVM自定义控件command双击事件
解决方案 »
- 【新人请教】dataGridView 问题!
- 用C# 如何将Word文档里的一个段落的前几个字改变格式,后面的字保持原样,谢谢。
- 50求一个正则表达式,很简单的,在线等
- SOS!!!!dataGridView问题,高手快来 最后50分了
- 如何将Excel中的数据导入access中?
- 不显示删除回复显示所有回复显示星级回复显示得分回复 QQ空间自定义管理如何实现?(版式/布局,模块,风格,自定义装扮,一键装扮)
- 关于地理信息系统的等高线或雨量线问题(已知X和Y坐标和高程(多点)或雨量(多个雨量站),如何划出等高线或雨量线)
- dataset 或 datatable merge() 后不能更新数据库是什么原因
- 请开发过工作流的朋友帮我看看这个中国移动项目的需求.(急)
- Ado.net EF 6.0 使用where的时候生成的SQL中为什么会有子查询
- C# 输入框组件如何实现自动产生千分位的逗号
- 任意数相加算法问题
command绑定就是不普通的mvvm绑定 自己找下例子吧
这个是一个自定义的控件 分CS 和XAML
然后你想在什么地方的什么事件触发它 直接在那个事件调用这个ICommand的Execute方法核心就是这样 其他的可以自己随便玩
关于mvvm 你使用的是开源框架prism mvvmlight还是自己写的
如果是框架 里面有很多例子
如果自己写的话 楼上说的很对 写个参数是Icommand类型的
然后执行的时候execute方法就行demo就不写了 很多东西自己不写一遍永远也不会
如果这些个东东楼主都不愿意动手练习的话 很难提高
自己动手丰衣足食~~~~ public class RelayCommand : ICommand
{
#region Fields readonly Action<object> _execute;
readonly Predicate<object> _canExecute; #endregion // Fields #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"); _execute = execute;
_canExecute = canExecute;
} #endregion // Constructors #region ICommand Members [DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
} public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
} public void Execute(object parameter)
{
_execute(parameter);
} #endregion // ICommand Members
在vm的command里面这样用
private ICommand _submitCmd;
public ICommand CMD_Submit
{
get
{
if (_submitCmd == null)
{
_submitCmd = new RelayCommand(
param => this.Submit(),
param => this._devices.Count==1
? true
: (this._devices.Count(i=>i.IsProcessed)==this._devices.Count)
);
}
return _submitCmd;
}
}代码自己理解下吧 用了蛮多的表达式的 希望楼主能看懂 应该不难得哦
我想问下,如果我想Command的触发事件是双击,我是不是还得写一个双击的路由事件呢?
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:NeiMLabel"
Title="Window1" Height="300" Width="300" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006" mc:Ignorable="d">
<Grid Background="AliceBlue" >
<uc:UCLabel x:Name="uclabel" stateColor="{Binding}" Command="{Binding _sc1._connectCommand}" Margin="102,23,116,208" />
<Button Content="ChangeColor" Height="34" HorizontalAlignment="Left" Margin="85,117,0,0" Name="button1" VerticalAlignment="Top" Width="128" Click="button1_Click" />
</Grid>
</Window>调用者的.xaml.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NeiMLabel.ViewModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;namespace NeiMLabel
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
_conCommand = new RelayCommand<string>(connectCommandFun);
uclabel.DataContext = _sc1;
_sc1 = new StateColorViewModel();
_sc1.sColor = Brushes.Yellow;
_sc1.connectCommand = _conCommand;
} public StateColorViewModel _sc1;
public StateColorViewModel sc1
{
set { _sc1 = value; }
get { return _sc1; }
} private void button1_Click(object sender, RoutedEventArgs e)
{
Random rd = new Random();
switch (rd.Next(4))
{
case 0:
_sc1.sColor = Brushes.Purple;
break;
case 1:
_sc1.sColor = Brushes.White;
break;
case 2:
_sc1.sColor = Brushes.Blue;
break;
case 3:
_sc1.sColor = Brushes.Yellow;
break;
case 4:
_sc1.sColor = Brushes.Green;
break;
default:
break; }
} public ICommand _conCommand { get; private set; } public void connectCommandFun(string obj)
{
int abd = 99;
}
}
}
自定义控件的xmal<UserControl x:Class="NeiMLabel.UCLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:NeiMLabel.ViewModel"
Height="30" Width="60">
<UserControl.DataContext>
<vm:StateColorViewModel x:Name="scvm"/>
</UserControl.DataContext>
<Grid>
<Border CornerRadius="4,4,4,4" BorderThickness="1,1,1,1" BorderBrush="#FFFEFEFE">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF9F7F7" Offset="0" />
<GradientStop Color="#FF3B62F2" Offset="1" />
<GradientStop Color="#FF4F83F6" Offset="0.364" />
</LinearGradientBrush>
</Border.Background>
<Grid VerticalAlignment="Center" HorizontalAlignment="Center" Width="60" Height="30">
<Ellipse Name="ellipsebg" Fill="{Binding sColor}" Stroke="{x:Null}" Width="20" Height="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Ellipse>
<!--<TextBlock Name="textblock" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="10" ></TextBlock>-->
</Grid>
</Border>
</Grid>
</UserControl>
自定义控件的.xaml.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NeiMLabel;
using NeiMLabel.ViewModel;
using System.Reflection;
namespace NeiMLabel
{
/// <summary>
/// UCLabel.xaml 的交互逻辑
/// </summary>
public partial class UCLabel : UserControl
{
public UCLabel()
{
InitializeComponent();
InputBinding inputBinding = new InputBinding(Command, new MouseGesture(MouseAction.LeftDoubleClick));
}
//定义依赖属性stateColor
public static readonly DependencyProperty stateColorProperty =
DependencyProperty.Register("stateColor", typeof(StateColorViewModel), typeof(UCLabel));
public StateColorViewModel stateColor
{
set { SetValue(stateColorProperty, value); }
get { return (StateColorViewModel)GetValue(stateColorProperty); }
}
//定义一个ICommand类型的依赖属性
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(UCLabel), new PropertyMetadata(null, null));
public ICommand Command
{
set { SetValue(CommandProperty, value); }
get { return (ICommand)GetValue(CommandProperty); }
}
}
}
自定义控件的vmusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Input;
using System.Diagnostics;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.IO;
using System.Windows;
using System.Text.RegularExpressions;
namespace NeiMLabel.ViewModel
{
public class StateColorViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private Brush _sColor;
public Brush sColor
{
set
{
_sColor = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("sColor"));
}
}
get { return _sColor; }
}
public StateColorViewModel()
{
_sColor = Brushes.Gray;
}
private ICommand _connectCommand;
public ICommand connectCommand
{
set
{
_connectCommand = value;
if (PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("connectCommand"));
}
}
get
{
return _connectCommand;
}
}
}
}先在此谢谢大家了,我才接触WPF,很多不清楚,请你们帮忙!
我在网上找到了一个比较符合要求的参考代码。如果有人和我有相近的需求,可以去看看这个
http://www.cnblogs.com/xiwang/archive/2012/07/23/2604924.html