概述
DataGridDataGrid中使用按钮并使用MVVM进行值绑定
1.界面如下
2.WPF前端代码
<Window x:Class="RetTest.Views.SetPathWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RetTest.Views"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator},Path=SetPaths}"
Title="设置路径" Height="700" Width="600" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Grid Width="600">
<DataGrid Name="DATA_GRID"
ItemsSource="{Binding Path=SetPathsConfig,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Margin="0,0,0,0" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="文件类型" Binding="{Binding Path=FileTypes ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.15*" />
<DataGridTextColumn Header="文件号" Binding="{Binding Path=FileNumbers,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.1*" />
<DataGridTextColumn Header="路径" Binding="{Binding Path=FilePaths,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.3*"/>
<DataGridTemplateColumn Header="操作" Width="0.1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button Content="导入" Width ="80" FontSize="14" CommandParameter="{Binding}" Command="{Binding Path=DataContext.FileClick,ElementName=DATA_GRID}"></Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</StackPanel>
</Grid>
</Window>
3.后端绑定代码
/*
* 编辑于2022-12-6
* 编辑人:
* 软件:Visual Studio2019
*/
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using RetTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RetTest.ViewModel
{
public class SetPathsViewModel : ViewModelBase
{
public SetPathsViewModel()
{
FileClick = new RelayCommand<SetPathsModel>(fileClick);
//Submits = new RelayCommand(submits);
SetData();
SetPathsConfig = Definitions.SetPaths;//这个是list值
}
#region 设置数据
/// <summary>
/// 设置数据
/// </summary>
private void SetData()
{
Definitions.SetPaths = new List<SetPathsModel>();
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元1配置文件",
FileNumbers = "9",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元2配置文件",
FileNumbers = "10",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元3配置文件",
FileNumbers = "11",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元4配置文件",
FileNumbers = "12",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元5配置文件",
FileNumbers = "13",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元6配置文件",
FileNumbers = "14",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元7配置文件",
FileNumbers = "15",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元8配置文件",
FileNumbers = "16",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元9配置文件",
FileNumbers = "17",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元10配置文件",
FileNumbers = "18",
FilePaths = ""
}); Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元11配置文件",
FileNumbers = "19",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元12配置文件",
FileNumbers = "20",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元13配置文件",
FileNumbers = "21",
FilePaths = ""
});
Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元14配置文件",
FileNumbers = "22",
FilePaths = ""
}); Definitions.SetPaths.Add(new SetPathsModel()
{
FileTypes = "单元15配置文件",
FileNumbers = "23",
FilePaths = ""
});
}
#endregion
public RelayCommand<SetPathsModel> FileClick { get; set; }
public RelayCommand Submits { get; set; }
private List<SetPathsModel> setPathsConfig;
public List<SetPathsModel> SetPathsConfig
{
get
{
return setPathsConfig;
}
set
{
//retConfig = value;
//RaisePropertyChanged("RetConfig");
Set(ref setPathsConfig, value);
}
}
public void fileClick(SetPathsModel setPaths)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "文件|*.bin;*.fxt";
fd.RestoreDirectory = true;
fd.InitialDirectory = Environment.CurrentDirectory;
DialogResult dr = fd.ShowDialog();
if (dr == DialogResult.OK || dr == DialogResult.Yes)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
Definitions.SetPaths[Convert.ToInt32(setPaths.FileNumbers) - 1].FilePaths = fd.FileName;
}));
}
else
{
MessageBox.Show("请选择文件");
}
}
//public void submits()
//{
// MessageBox.Show("修改");
//}
}
}
最后
以上就是犹豫香菇为你收集整理的WPF中在DataGrid中使用按钮并使用MVVMLight进行值绑定的全部内容,希望文章能够帮你解决WPF中在DataGrid中使用按钮并使用MVVMLight进行值绑定所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复