我使用的是.NET 4.0.我有一些非常简单的代码,允许用户输入1到99,999(含)之间的数字.我在Property setter中有一些逻辑,如果它不遵守业务规则(例如,它不是数字或数字太大),则会阻止应用最新值.
public class MainViewModel : INotifyPropertyChanged
{
#region Fields
private string _text = string.Empty;
#endregion // Fields
#region Properties
public string Text
{
get { return _text; }
set
{
if (_text == value) return;
if (IsValidRange(value))
{
_text = value;
}
OnPropertyChanged("Text");
}
}
#endregion // Properties
#region Private Methods
private bool IsValidRange(string value)
{
// An empty string is considered valid.
if (string.IsNullOrWhiteSpace(value)) return true;
// We try to convert to an unsigned integer, since negative …Run Code Online (Sandbox Code Playgroud) 我有多个控件,包括TextBox和ComboBox,我希望所有控件都显示一个ToolTip,其中包含Validation.Errors集合中包含的所有错误.如果可能的话,我希望他们都能分享一种共同的风格,这就是我的努力.我确信我在ToolTip setter中的绑定有问题,但我无法弄清楚是什么.我在INotifyDataErrorInfo实现中返回一个Error对象,该对象指定错误的严重性(错误或警告).
我希望有一个适用于Window中所有控件的样式,它将显示一个ToolTip,其中包含该控件的所有错误和警告的列表.错误应显示为红色,警告显示为黄色.这是我提出的风格:
<Style TargetType="FrameworkElement">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors), RelativeSource={RelativeSource Self}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent.ErrorMessage}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(Validation.HasError)}" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent.ErrorMessage}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate> …Run Code Online (Sandbox Code Playgroud) 背景
我有一个类,它使用 NHibernate 将对象保存到数据库中。当您调用MergeEntity没有设置 ID 的对象时,NHibernate 在返回时会用 ID 填充该对象。为了确保我始终使用与 NHibernate 使用的对象相同的对象,我将更新后的对象从我的“ Save”函数传回。
问题
我试图使用 Moq 来模拟相同的行为,这通常非常直观且易于使用;但是,我在验证是否Save()使用正确的参数进行调用时遇到了一些麻烦。我想验证传入的对象的 ID 是否为零,然后该函数是否已正确设置它Save。不幸的是,当我在函数中修改ID时Moq.Returns(),Moq.Verify函数使用修改后的值而不是传入的ID值。
为了说明这一点,这是一个非常基本的类(我重写了 ToString() 函数,因此我的测试输出将显示调用模拟的 Save() 时使用的 ID):
public class Class1
{
private readonly IPersistence _persistence;
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
public Class1(IPersistence persistence)
{
_persistence = persistence;
}
public int Id { get; set; }
public void Save()
{
_persistence.Save(this);
}
public override string ToString()
{ …Run Code Online (Sandbox Code Playgroud) 我有一个包含两列的DataGrid:
我已经设置了数据验证,以便如果有一个值,另一个将出错,直到它也有一个值.验证很愚蠢,但它提供了一些简单的标准来进行验证,因此我可以说明这个问题.
当我在文本单元格中输入内容时,按Tab键,然后单击第一个单元格,第一个单元格显示它处于错误状态(这是正确的).问题是,当我从组合框下拉列表中选择某些内容并离开该单元格时(通过按Tab键或单击另一个单元格),我为组合框选择的值将消失.我有绑定集来在属性更改时更新我的源代码,因此一旦我选择它就会设置为我选择的值.但是,当我离开单元格时,属性被设置为null.如果单元格未处于错误状态,我不会看到此行为.
有人可以帮忙吗?这是我的DataGrid的XAML:
<DataGrid Grid.Row="2"
Name="GrdData"
ItemsSource="{Binding Path=Dvm.Data}"
SelectedItem="{Binding Path=Dvm.SelectedData, Mode=TwoWay}"
CanUserAddRows="True"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
Margin="5"
SelectionMode="Single"
IsEnabled="{Binding Path=IsGridEnabled}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Column 1"
SelectedItemBinding="{Binding Path=Col1, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Width="*"
DisplayMemberPath="Description">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}"/>
<Setter Property="IsDropDownOpen" Value="True" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Column 2"
Binding="{Binding Path=Col2, Mode=TwoWay, ValidatesOnDataErrors=True}"
Width="*"/>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
我无法想象我做错了什么.我看到这个似乎描述了我遇到的同样问题的其他链接,但是对他们起作用的解决方案对我来说似乎不起作用; 我添加了SelectedValueBinding和SelectedValuePath,但行为没有改变.
我有一个带有属性的类required。我创建了一个复制构造函数,将该属性初始化为该类的源实例所具有的相同内容。为什么编译器给我这个错误:
CS9035:必须在对象初始值设定项或属性构造函数中设置必需的成员“PropertyName”。
public class Class1
{
public required string Name {get; set;}
public Class1(){}
public Class1(Class1 src)
{
Name = src.Name;
}
}
public class OtherClass
{
public void func()
{
// This works fine
var data = new Class1
{
Name = "test"
};
// This gives compiler error
var data1 = new Class1(data);
}
}
Run Code Online (Sandbox Code Playgroud) 在 SpecFlow步骤定义的文档中,它显示了一个示例,您可以在场景中使用逗号分隔列表在步骤定义中创建列表:
例如:
鉴于我“周二、周五、周日”有空
会导致:
@Given("I am available on \"(.+)\"")
public void I_have_cukes_in_my_belly(List<String> days) {
// Do something with the days
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我这样做时,我得到了这个:
@Given("I am available on ""(.*)"")
public void I_have_cukes_in_my_belly(string days) {
// Do something with the days
}
Run Code Online (Sandbox Code Playgroud)
我使用的是 VS 2012 和 SpecFlow 1.9.1 版本。有谁知道我可能做错了什么?或者我该如何解决这个问题?
提前致谢。
c# ×5
wpf ×3
.net-4.0 ×1
.net-4.5 ×1
c#-11.0 ×1
data-binding ×1
moq ×1
specflow ×1
unit-testing ×1
validation ×1