在WPF的View-Model-ViewModel模式下,我试图为网格控件的各种定义的高度和宽度进行数据绑定,因此我可以在使用GridSplitter后存储用户设置它们的值.但是,正常模式似乎不适用于这些特定属性.
注意:我发布这个作为参考问题,我发布谷歌失败了我,我不得不自己解决这个问题.我自己的答案可以遵循.
如果我创建了对该IsReadOnly属性的绑定DataGridTextColumn,则它不会实现.如果我通过标记设置它,它的工作原理.
<DataGridTextColumn IsReadOnly="{Binding IsReferenceInactive}"/> <!-- NOP -->
<DataGridTextColumn IsReadOnly="True"/> <!-- Works as expected, cell is r/o -->
Run Code Online (Sandbox Code Playgroud)
该IsReferenceInactive属性是一个DP并且工作正常(出于测试目的,我将它绑定到一个复选框,这有效)
这是一个已知的限制吗?
更新
Uups,除了我写的,输出窗口中有一条消息:
<DataGridTextColumn IsReadOnly="{Binding IsReferenceInactive}"/> <!-- NOP -->
<DataGridTextColumn IsReadOnly="True"/> <!-- Works as expected, cell is r/o -->
Run Code Online (Sandbox Code Playgroud)
似乎是这个:
假设您有一个继承自ValidationRule的类:
public class MyValidationRule : ValidationRule
{
public string ValidationType { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中,您正在验证如下:
<ComboBox.SelectedItem>
<Binding Path="MyPath" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding.ValidationRules>
<qmvalidation:MyValidationRule ValidationType="notnull"/>
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedItem>
Run Code Online (Sandbox Code Playgroud)
哪个有效,一切都好.
但是现在假设,你想拥有ValidationType="{Binding MyBinding}"它的MyBinding来源DataContext.
为此,我需要创建MyValidationRule一个DependencyObject并添加一个依赖属性.
我试着写一个类DependencyObject,然后绑定它.但是有两个问题.. ValidationRule没有DataContext来自Combobox/Item的.
你有什么想法,怎么解决?
谢谢 !
我有一个 TreeListControl 绑定到我的虚拟机中的一个集合。我还想在树列表控件内定义上下文菜单,使其标题文本绑定到虚拟机中的另一个字符串。在这种情况下如何设置数据上下文?我尝试过了
<Window.DataContext>
<model:ViewModel></model:ViewModel>
</Window.DataContext>
<Grid>
<Button Grid.Row="1" Command="{Binding CellCheckedCommand}"></Button>
<TextBlock Text="{Binding HeaderText}" Grid.Row="2">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext}" Header="{Binding HeaderText}"></MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
这是视图模型
public DelegateCommand CellCheckedCommand { get; set; }
private String _HeaderText;
public String HeaderText
{
get
{
return _HeaderText;
}
set
{
_HeaderText = value;
NotifyPropertyChanged("HeaderText");
}
}
public void NotifyPropertyChanged(String name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private void CellCheckedMethod()
{
HeaderText = "Changed";
}
Run Code Online (Sandbox Code Playgroud) 我想使用自定义对象列表在菜单中生成MenuItems列表,但是在该菜单的底部,我希望始终显示几个静态MenuItems。从逻辑上讲,这可以通过以编程方式创建另一个要绑定的列表来完成,该列表始终将那些MenuItems放在底部,但这让我感到有些天真。我敢肯定,对于已经存在的列表以及一些XAML向导,可能还有某种DataTemplate,还有一种更优雅的方法。有指针吗?
下面是一个最小的例子,我不可能再减少它了。
我在 ViewModel 中创建了一个实时过滤的 CollectionView,如下所示:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;
namespace AntiBonto.ViewModel
{
[Serializable]
public class Person
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public string Name { get; set; }
public override string ToString()
{
return Name;
}
private int num;
public int Num
{
get { return num; }
set { num = value; RaisePropertyChanged(); }
} …Run Code Online (Sandbox Code Playgroud) wpf ×6
.net ×2
c# ×2
data-binding ×2
binding ×1
c#-4.0 ×1
column-width ×1
datacontext ×1
datagrid ×1
grid ×1
row-height ×1
validation ×1
xaml ×1