我完全按照以下链接中的描述实现了INotifyDataErrorInfo:
http://blog.micic.ch/net/easy-mvvm-example-with-inotifypropertychanged-and-inotifydataerrorinfo
我有一个TextBox绑定到我的模型中的字符串属性.
XAML
<TextBox Text="{Binding FullName,
ValidatesOnNotifyDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
模型
private string _fullName;
public string FullName
{
get { return _fullName; }
set
{
// Set raises OnPropertyChanged
Set(ref _fullName, value);
if (string.IsNullOrWhiteSpace(_fullName))
AddError(nameof(FullName), "Name required");
else
RemoveError(nameof(FullName));
}
}
Run Code Online (Sandbox Code Playgroud)
INotifyDataError代码
private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
// get errors by property
public IEnumerable GetErrors(string propertyName)
{
if (_errors.ContainsKey(propertyName))
return _errors[propertyName];
return null;
}
public bool HasErrors => _errors.Count > …Run Code Online (Sandbox Code Playgroud) 我有一个pandas数据框,其中包含3列,每列包含用户在会话期间访问过的站点.
在某些情况下,用户可能没有在一个会话中访问过3个站点.这由0表示,表示没有访问过任何站点.
import pandas as pd
df = pd.DataFrame(data=[[5, 8, 1],[8,0,0],[1,17,0]],
columns=['site1', 'site2', 'site3'])
print(df)
site1 site2 site3
0 5 8 1
1 8 0 0
2 1 17 0
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,用户0访问了站点5,8和1.用户1仅访问了站点8,用户2访问了站点1和17.
我想创建一个新列,last_site它显示用户在该会话中访问的最后一个站点.
我想要的结果是:
site1 site2 site3 last_site
0 5 8 1 1
1 8 0 0 8
2 1 17 0 17
Run Code Online (Sandbox Code Playgroud)
如何使用熊猫以简洁的方式做到这一点?
我已经声明了以下类型:
public interface ITest { }
public class ClassOne : ITest { }
public class ClassTwo : ITest { }
Run Code Online (Sandbox Code Playgroud)
在我的viewmodel中,我正在声明并初始化以下集合:
public class ViewModel
{
public ObservableCollection<ITest> Coll { get; set; } = new ObservableCollection<ITest>
{
new ClassOne(),
new ClassTwo()
};
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我宣布以下内容 ItemsControl
<ItemsControl ItemsSource="{Binding Coll}">
<ItemsControl.Resources>
<DataTemplate DataType="local:ClassOne">
<Rectangle Width="50" Height="50" Fill="Red" />
</DataTemplate>
<DataTemplate DataType="local:ClassTwo">
<Rectangle Width="50" Height="50" Fill="Blue" />
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我期待看到的是一个红色方块,后面是一个蓝色方块,而我所看到的是以下内容:
我究竟做错了什么?
我在解决以下练习时遇到问题,我会感激任何帮助.
设Σ= {a,b}.我需要为包含奇数a的所有字符串提供正则表达式.
感谢您的时间
我正在使用AutoSuggestBox控件来显示一些结果,如下所示:
<AutoSuggestBox Width="192"
PlaceholderText="Search"
HorizontalAlignment="Right"
ItemsSource="{Binding SearchResults}">
<i:Interaction.Behaviors>
...
</i:Interaction.Behaviors>
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Name}" />
<Run Text="(" /><Run Text="{Binding Origin_Country[0]}" /><Run Text=")" />
</TextBlock>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
SearchResults(ItemsSource绑定)定义如下:
private ObservableCollection<ShowModel> _searchResults = default(ObservableCollection<ShowModel>);
public ObservableCollection<ShowModel> SearchResults { get { return _searchResults; } set { Set(ref _searchResults, value); } }
Run Code Online (Sandbox Code Playgroud)
而ShowModel是具有可绑定属性的基本模型.
我遇到的问题是,当我点击其中一个结果时,它正在填充文本框中的模型路径,如下所示:
我想要的是为文本框定义某种模板以绑定到模型的某个属性,这样就不会显示模型路径.这甚至可能吗?
我正在使用微服务架构创建系统。有两个微服务A和B,每个都位于自己的存储库中。
有一个user.proto包含protobuf定义和gRPC方法签名的文件。用作服务器的A用途user.pb.go。B使用user.pb.go作为(一个客户A)。
一种构造方法是在中出现原型定义A,并B具有对以下内容的代码依赖性A:
A
??? pb
? ??? user.proto
? ??? user.pb.go
??? service.go
B
??? service.go
B-->A
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用另一个P包含原始定义的回购,A并B取决于新的回购:
A
??? service.go
B
??? service.go
P
??? user.proto
??? user.pb.go
A-->P
B-->P
Run Code Online (Sandbox Code Playgroud)
或者,新的仓库可能只包含原始文件,并且在A和B中都生成了代码:
A
??? service.go
??? pb
??? user.pb.go
B
??? service.go
??? pb
??? user.pb.go
P
??? user.proto
Run Code Online (Sandbox Code Playgroud)
这里有什么更好的方法?
我正在尝试在 bash 中设置以下环境变量:
ConnectionStrings:DefaultConnection=someValue
我正在使用以下命令:
export ConnectionStrings:DefaultConnection=something
在 bash 中,我收到以下错误:
export: 'ConnectionStrings:DefaultConnection=something': not a valid identifier
在 zsh 中出现以下错误:
export: not valid in this context: ConnectionStrings:DefaultConnection
如何设置变量名包含冒号的环境变量?
我有一个TextBlock和一个CheckBox,例如:
<StackPanel >
<TextBlock Text="Colors"/>
<CheckBox Content="Blue" IsChecked="{Binding Model.Blue, ValidatesOnNotifyDataErrors=False}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我正在实现INotifyDataErrorInfo并验证是否选中了该复选框。如果未检查,则将其视为错误:
public class MyModel : INotifyPropertyChanged, INotifyDataErrorInfo
{
[CustomValidation(typeof(MyModel), "CheckBoxRequired")]
public bool Blue
{
get { return _blue; }
set { _blue = value; RaisePropertyChanged(nameof(Blue)); }
}
public static ValidationResult CheckBoxRequired(object obj, ValidationContext context)
{
var model = (MyModel)context.ObjectInstance;
if (model.Blue == false)
return new ValidationResult("Blue required", new string[] { "Blue" });
else
return ValidationResult.Success;
}
//...
//INotifyPropertyChanged & INotifyDataErrorInfo implementations omitted
} …Run Code Online (Sandbox Code Playgroud) 我正在使用gorm映射我的数据库。
我有两个具有多对多关系的表(service和resource)。我在代码中对它们进行建模,如下所示:
type Service struct {
BaseModel
Name string `gorm:"not null;unique_index"`
Resources []Resource `gorm:"many2many:service_resource"`
}
type Resource struct {
BaseModel
Name string `gorm:"not null;unique_index"`
}
Run Code Online (Sandbox Code Playgroud)
使用 gorm 的AutoMigrate创建以下表:
(我还执行了一个原始 SQL 查询以在映射表中添加 id 主键。)
要创建新服务,我使用以下代码:
service := Service{
Name: "core",
Resources: []Resource{
{Name: "ec2"},
{Name: "dynamo"},
},
}
db.Create(&service)
Run Code Online (Sandbox Code Playgroud)
这将创建所有资源以及服务service_resource,并按预期将它们之间的关系填充到表中。
但是,我的问题是当我查询服务时。我使用以下代码来检索所有服务:
services := []model.Service{}
db.Find(&services)
Run Code Online (Sandbox Code Playgroud)
这在填充了服务数组的情况下成功返回,但Resources每个服务的数组为空:
"services": [
{
"ID": 1,
"Name": "core",
"Resources": null
},
...
]
Run Code Online (Sandbox Code Playgroud)
我假设 …