如何在属性网格中加载xml文档并将xml标记显示为属性网格中的属性
对于上面的问题,我在属性网格中得到了答案(通过Marc Gravell代码)但是由于可扩展对象转换器我得到了所有属性的"+"我需要删除它,如何删除
以下人为的例子说明了我的问题.
我的ViewModel:
public class MyViewModel : ViewModel
{
public MyViewModel()
{
var table = new DataTable("MyDatatable");
table.Columns.Add("Some Val", typeof(double));
table.Columns.Add("Ref");
var row = table.NewRow();
row["Some Val"] = 3.14;
row["Ref"] = new {Title = "My Title", Description = "My Description"};
table.Rows.Add(row);
MyDataView = table.DefaultView;
}
DataView MyDataView { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题在于我的Xaml代码.我希望我的一个列标题为"我的标题",相应的行值为"我的描述"...
我的Xaml:
<DataGrid ItemsSource="MyDataView" AutoGenerateColumns="False">
<DataGrid.Columns>
<!--This column works fine. -->
<DataGridTextColumn Header="Some Val" Binding="{Binding 'Some Val'}" Width="50"/>
<!--This column doesn't work... I'm looking for something similar to this: …Run Code Online (Sandbox Code Playgroud) 我的sql语句中有一个情况.在这个sql语句中,我一起加入了3个表(Application_Detail,Teacher_Detail和Class_Detail),而且我过去WHERE用来过滤我的表以找出有多少老师使用过这个应用程序,我发现了以下结果.
正如您在第1条记录中所看到的,教师相关字段和类相关字段均为空.我想知道有没有办法过滤掉第一条记录,只显示2,3,4记录?因为我想只显示记录,如果在teacherId,teacherName,class或grade列中有一些值.
teacherId teacherName applicationName class grade
1. NULL NULL Excel NULL NULL
2. 5 NULL Excel NULL NULL
3. NULL NULL Excel A 6
4 NULL NULL Excel B 2
Run Code Online (Sandbox Code Playgroud)
这是我的SQL命令
SELECT
td.teacherId,
teacherName,
applicationName,
class,
grade
FROM
[AppUser_Detail] as aud
LEFT OUTER JOIN [Teacher_Detail] as td ON aud.teacherId = td.teacherId
LEFT OUTER JOIN [Application_Detail] as ad ON aud.applicationId = ad.applicationId
LEFT OUTER JOIN [Class_Detail] as cd ON aud.classId = cd.classId
WHERE
aud.applicationId = 6 …Run Code Online (Sandbox Code Playgroud) 我必须访问DataGrid的活动单元格下面的值(围绕它的黑色边框的单元格).
幸运的是DataGrid有很多属性,比如CurrentCell,CurrentItem SelectedCells SelectedItem和SelectedItems似乎为我提供了我想要的数据.
但是,我还没有弄清楚如何以简单的方式访问单元格.我也改变了......
SelectionMode="Single" SelectionUnit="Cell"
Run Code Online (Sandbox Code Playgroud)
......属性但最后我必须做这样的事情:
DataGridCellInfo cellInfo = dataGrid.CurrentCell;
if(null != cellInfo && cellInfo.IsValid)
{
object[] array = cellInfo.Item as object[];
if (null != array && cellInfo.Column.DisplayIndex >= 0 && cellInfo.Column.DisplayIndex < array.Length)
{
object cellValue = array[cellInfo.Column.DisplayIndex];
if (null != cellValue)
{
// Here we are
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,该行是通过包含各种对象类型的对象数组构建的.我知道我可以在cellInfo.Column上执行绑定(在演员之后),但这不是重点.我的问题是,如果我做了一些非常错误的事情,因为我无法像DataGrid那样成像那么强大的软件,如果不进行如此大量的编码就无法为我提供所需的价值.
我错过了什么,或者获得当前单元格值是否真的如此复杂.
UPDATE
正如Quartermeister在他非常好的答案中解释的那样,没有一个属性可以访问单元格值,这是有意义的原因.另外,如果你让用户重新安排列,请注意像我在我的例子中那样使用DisplayIndex.
我正在尝试将样式应用于组合框,但组合框本身并没有应用,而是消失了。请检查以下用于用户控制的 xaml 代码。
<用户控制
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes =“clr命名空间:Microsoft.Windows.Themes;程序集=PresentationFramework.Luna”
x:Class="Guardian.PAS.PASFramework.UI.WPF.PASComboBox"
xmlns:local="clr-命名空间:Guardian.PAS.PASFramework.UI.WPF"
高度=26 宽度=100 垂直对齐=中心>
<用户控件.资源>
<Style x:Key="comboBoxStyle" TargetType="{x:Type local:PASCustomComboBox}">
<设置属性=“模板”>
<设置器.值>
<ControlTemplate TargetType="{x:Type local:PASCustomComboBox}">
<控制模板.触发器>
<触发器属性=“本地:PASCustomComboBox.IsEnabled”值=“假”>
<Setter属性=“背景”值=“红色”/>
</触发器>
</ControlTemplate.触发器>
</控制模板>
</Setter.Value>
</设置>
</风格>
</用户控件.资源>
<画布名称=“画布”高度=“23”宽度=“自动”垂直对齐=“居中”>
<标签高度=“23”名称=“lblCaption”宽度=“20”垂直对齐=“中心”>aaa</标签>
<本地:PASCustomComboBox高度=“23”x:名称=“cmbComboBoxControl”VerticalAlignment=“中心”宽度=“50”
IsEditable="True" Style="{StaticResourcecomboBoxStyle}">
</本地:PASCustomComboBox>
<按钮高度=“23”名称=“btnSearch”宽度=“25”点击=“btnSearch_Click”可见性=“折叠”
VerticalAlignment="中心">...</按钮>
<标签高度=“23”名称=“lblDescription”垂直对齐=“中心”宽度=“20”前景=“蓝色”>
</标签>
</画布>
</用户控制>
这里 PASCustomComboBox 是一个继承自组合框的类。
public class PASCustomComboBox : ComboBox
{
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Down || e.Key == Key.Up)
{
e.Handled = true;
return;
}
base.OnPreviewKeyDown(e);
}
}
Run Code Online (Sandbox Code Playgroud) 首先,道歉,因为我确信这是问题的答案很简单.尽管如此,我似乎仍无法找到答案.
这是我使用WPF的第一周.我只想在DataGrid中显示某种列表的内容.我目前正在尝试使用ObservableCollection <>和DataGrid,但这可能会改变.我如何DataTemplate列表并显示它?
该列表位于ViewModel中,该ViewModel已在Apps.xaml.cs中设置为MainWindow.xaml的DataSource:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var window = new MainWindow();
// Create the ViewModel to which
// the main window binds.
var viewModel = new MainWindowViewModel();
// Allow all controls in the window to
// bind to the ViewModel by setting the
// DataContext, which propagates down
// the element tree.
window.DataContext = viewModel;
window.Show();
}
Run Code Online (Sandbox Code Playgroud)
这是当前列表:
public ObservableCollection<PersonData> SearchResults { get; set; }
Run Code Online (Sandbox Code Playgroud)
至于我的xaml,我相当迷失 - 我试图摆弄绑定和ItemsSource,并且真的不知道如何在这里使用它们.另外,我认为使用DataTemplate是必要的,因为我需要让它知道列需要显示PersonData的某些属性,例如名字和姓氏,位置和标题.任何帮助表示赞赏.
编辑
更一般地说 - 如何只显示一个ViewModel列表,集合,你有什么,期间?
很多道歉要添加到类似的绑定错误问题的大量列表,但经过几个小时的搜索后,我找不到适合我的解决方案!
我试图在WPF中测试ViewModel,但ICommand Button代码导致标准绑定错误:
System.Windows.Data错误:40:BindingExpression路径错误:'对象'''HubManagerViewModel'(HashCode = 13328197)'上找不到'CheckforNewHubs'属性.BindingExpression:路径= CheckforNewHubs; DataItem ='HubManagerViewModel'(HashCode = 13328197); target元素是'Button'(Name ='CheckForNewHubsButton'); target属性是'Command'(类型'ICommand')
我的命令(在HubManagerViewModel中)是:
public ICommand CheckForNewHubs
{
get
{
return new RelayCommand(this.CheckForNewHubsExecute, this.CanSendHubManagerCommands);
}
set { }
}
Run Code Online (Sandbox Code Playgroud)
我的DataContext在XAML网格中启动,如下所示:
<Grid.DataContext>
<ViewModels:HubManagerViewModel/>
</Grid.DataContext>
Run Code Online (Sandbox Code Playgroud)
我试过显式设置按钮的dataContext,如下所示:
<Button.DataContext>
<ViewModels:HubManagerViewModel/>
</Button.DataContext>
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?在这一点上,我只是渴望一些新的见解.
感谢您提前的时间!
我正在尝试制作 PropertyGrid 自定义控件。PropertyGrid 与 Visual Studio 中使用的 PropertyGrid 非常相似。我尝试使用扩展 WPF 工具包的 PropertyGrid,但您必须使用属性指定属性的类别,并且我们需要在运行时更改类别。据我所知,这对于属性来说是不可能的。
所以我自己制作PropertyGrid。到目前为止,这是我的代码:
Generic.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HomeMadePropertyGrid"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<BooleanToVisibilityConverter x:Key="BoolToVisConverter"></BooleanToVisibilityConverter>
<SolidColorBrush x:Key="GlyphBrush" Color="#444" />
<ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
<Grid Width="15" Height="13" Background="Transparent">
<Path x:Name="ExpandPath" Fill="{StaticResource GlyphBrush}" Data="M 4 0 L 8 4 L 4 8 Z" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1,1,1,1" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="ExpandPath" Value="M 0 4 L 8 4 L 4 8 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
</Style> …Run Code Online (Sandbox Code Playgroud) 我有一个数据网格,它绑定到一个collectionviewsource,它绑定到一个observablecollection。在遵循指南的过程中,我将其设置如下:
我的人物课程:
public class Persons : ObservableCollection<Person>
{
//...
}
Run Code Online (Sandbox Code Playgroud)
xaml数据绑定:
<Window.Resources>
<local:Persons x:Key="_Persons"/>
<CollectionViewSource x:Key="cvsPersons" Source="{StaticResource _Persons}" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
数据网格绑定:
<DataGrid x:Name="myDataGrid" ItemsSource="{Binding Source={StaticResource cvsPersons}}"/>
Run Code Online (Sandbox Code Playgroud)
后面的代码:
_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];
cvsPersons.Source = _Persons;
Run Code Online (Sandbox Code Playgroud)
以上作品。 我的问题是,为什么我需要在后面使用cvsPersons.Source = _Persons;在代码中设置collectionviewsource.source?我以为我的第一段代码中的xaml可以完成这项工作:
_cvsPersons.Source = _Persons;
Run Code Online (Sandbox Code Playgroud)
如果我需要所有这些代码,那么xaml数据绑定代码似乎没什么用,我也可以在代码中做所有事情。根据我的(也许很少)理解,后面代码中唯一需要的代码是引用xaml设置的实例,即:
_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];
Run Code Online (Sandbox Code Playgroud)
如果我没有_cvsPersons.Source = _Persons; 那么我的数据网格将不会被填充。我的xaml不能正常工作。我想我的问题更多与概念有关。
我有一个简单的场景,我有一个绑定到ObservableCollection的DataGrid,我希望用户能够动态添加一个新的ComplexType项(这意味着一行)。我的意思是,每当用户完成输入行数据时,都需要更新数据模型。问题是更改或添加行时未调用ViewModel的绑定。有谁知道如何在ViewModel中处理这种情况。我的直觉告诉我,在MVVM中应该很容易,但是我被卡住了!这是我的xaml:
<DataGrid x:Name="gd_Users" CanUserDeleteRows="True" CanUserAddRows="True" CanUserAddRows="True" AutoGenerateColumns="False" ItemsSource="{Binding UserCollection,Mode=TwoWay}" SelectedItem="{Binding SelectedUser}" Margin="1,5" Grid.Column="1">
<DataGrid.Columns>
<DataGridTextColumn Header="header1" Width="50" Binding="{Binding u_Code}"/>
<DataGridTextColumn Header="Header2" Width="100" Binding="{Binding u_FirstName}"/>
<DataGridTextColumn Header="header3" Width="150" Binding="{Binding u_LastName}" />
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
PS:我不想在后面的代码中使用“ RowEditEnding”之类的事件
c# ×8
wpf ×8
datagrid ×4
xaml ×4
mvvm ×3
.net ×1
data-binding ×1
datatemplate ×1
itemscontrol ×1
sql ×1
sql-server ×1
winforms ×1
wpf-controls ×1
wpfdatagrid ×1
wpftoolkit ×1