WPF DataGrid中,怎么给DataGridTemplateColumn中的控件绑定ItemsSource对象中的属性?

测试出生的码农 2019-04-25 03:17:15
我现在有一个DataGrid,它的ItemsSource是绑定的一个ObservableCollection<ProgressStepModel>对象。
然后其中有一列需要包含一个CheckBox和一个TextBlock,我希望CheckBox的IsChecked属性从ProgressStepModel中来,要怎么写?
现在报错说“Cannot resolve property ItemIsChecked in data context of type ..........”


<DataGrid x:Name="GroupSearchDataGrid" DockPanel.Dock="Left" GridLinesVisibility="All" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
BorderThickness="0" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" CanUserAddRows="False"
ItemsSource="{Binding DataSource,Mode=TwoWay}" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
Style="{StaticResource DefaultDataGrid}" >
<DataGrid.Columns>
<DataGridTextColumn Header="队列号" Width="*" Binding="{Binding StepNum}" IsReadOnly="True" FontSize="16"/>
<DataGridTextColumn Header="项目编号" Width="*" Binding="{Binding ItemNum}" IsReadOnly="True" FontSize="16"/>
<DataGridTemplateColumn Width="*" MinWidth="100" Header="项目">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding ItemIsChecked}"></CheckBox>
<TextBlock Text="项目" FontFamily="微软雅黑" FontSize="16"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


public class ProgressStepModel
{
public int StepNum { get; set; }

public int ItemNum { get; set; }

public bool ItemIsChecked { get; set; }

public bool FuncIsChecked { get; set; }

public bool LogicIsChecked { get; set; }

public string Description { get; set; }

public bool SelectFuncVisibility { get; set; }

public bool SelectThresholdVisibility { get; set; }

public string ThresholdValue { get; set; }

public string Deviation { get; set; }
}
...全文
378 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
OrdinaryCoder 2019-04-25
  • 打赏
  • 举报
回复
我用的是Prism库 NotificationObject类实现了INotifyPropertyChanged接口 不用也可以自己实现INotifyPropertyChanged接口 例子网上应该都可以找到
OrdinaryCoder 2019-04-25
  • 打赏
  • 举报
回复

<UserControl x:Class="VisionPlugin.Views.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListView ItemsSource="{Binding Tests}">
<ListView.View>
<GridView>
<GridViewColumn Header="测试1">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox BorderBrush="Transparent" BorderThickness="0" IsChecked="{Binding IsChecked1, Mode=TwoWay}" IsEnabled="False" Width="70"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="测试2">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox BorderBrush="Transparent" BorderThickness="0" IsChecked="{Binding IsChecked2, Mode=TwoWay}" IsEnabled="False" Width="70"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="测试3">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox BorderBrush="Transparent" BorderThickness="0" IsChecked="{Binding IsChecked3, Mode=TwoWay}" IsEnabled="False" Width="70"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>



public class TestUserControlViewModel : NotificationObject
{
private ObservableCollection<TestModel> tests;

public ObservableCollection<TestModel> Tests
{
get { return tests; }
set
{
tests = value;
this.RaisePropertyChanged("Tests");
}
}
public TestUserControlViewModel()
{
tests = new ObservableCollection<TestModel>() {
new TestModel(){
IsChecked1 =true,
IsChecked2=false,
IsChecked3 =false,

},
new TestModel(){
IsChecked1 =false,
IsChecked2=false,
IsChecked3 =false,

},
new TestModel(){
IsChecked1 =false,
IsChecked2=true,
IsChecked3 =true,

},

};
}
}



public class TestModel : NotificationObject
{
private bool isChecked1;
public bool IsChecked1
{
get { return isChecked1; }
set
{
isChecked1 = value;
this.RaisePropertyChanged("IsChecked1");
}
}
private bool isChecked2;

public bool IsChecked2
{
get { return isChecked2; }
set
{
isChecked2 = value;
this.RaisePropertyChanged("isChecked2");
}
}
private bool isChecked3;

public bool IsChecked3
{
get { return isChecked3; }
set
{
isChecked3 = value;
this.RaisePropertyChanged("IsChecked3");
}
}


}

Xaml代码
ViewModel代码
Model代码
  • 打赏
  • 举报
回复
咦?虽然代码里面有错误提示,但是实际运行起来绑定是生效的,没有问题
  • 打赏
  • 举报
回复
引用 1 楼 OrdinaryCoder 的回复:

<GridViewColumn Header="拍照">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox BorderBrush="Transparent" BorderThickness="0" IsChecked="{Binding IsTakePicture, Mode=TwoWay}" IsEnabled="False" Width="70"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>



private bool isTakePicture;
public bool IsTakePicture
{
get { return isTakePicture; }
set
{
isTakePicture = value;
this.RaisePropertyChanged("IsTakePicture");
}
}

Model类需要继承NotificationObject

我这里是一个列表,里面每个元素都需要这个属性,而且都不一样,我是想每个元素都对应一个Model,元素的值从Model中来,要怎么实现?
OrdinaryCoder 2019-04-25
  • 打赏
  • 举报
回复

<GridViewColumn Header="拍照">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<CheckBox BorderBrush="Transparent" BorderThickness="0" IsChecked="{Binding IsTakePicture, Mode=TwoWay}" IsEnabled="False" Width="70"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>



private bool isTakePicture;
public bool IsTakePicture
{
get { return isTakePicture; }
set
{
isTakePicture = value;
this.RaisePropertyChanged("IsTakePicture");
}
}

Model类需要继承NotificationObject

8,833

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧