在WPF中,DataTrigger是否可以绑定到附加属性?
我本质上希望在附加属性上使用转换器,以在特定验证规则被破坏时提供样式.我正在使用如下标记:
<DataTrigger Binding="{Binding Path=Validation.Errors,
RelativeSource={RelativeSource Self},
Converter={StaticResource RequiredToBoolConverter}}"
Value="True">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
Run Code Online (Sandbox Code Playgroud)
但是,当这运行时,我得到以下内容:
System.Windows.Data错误:39:BindingExpression路径错误:'object'''TextBox'(Name ='')'上找不到'Validation'属性.BindingExpression:路径= Validation.Errors; DataItem ='TextBox'(Name =''); target元素是'TextBox'(Name =''); target属性是'NoTarget'(类型'对象')
如果我将DataTrigger绑定路径更改为"Text",我不会得到数据绑定错误(但当然它不提供我正在寻找的行为).
我试图解决是否有内置支持绑定复杂类型以形成元素.
使用一个常见的假设情况:我有一个属于类别的Product实体 - 模型看起来像这样:
public class Product
{
public int ID { get; set; }
public string Description { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int ID { get; set; }
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用ASP.Net MVC框架创建一个表单来水合一个只包含简单值类型的新实体是好的和简单的,例如:
public ActionResult Create(Product product);
Run Code Online (Sandbox Code Playgroud)
但是,您的实体包含其他复杂类型的上述情况又如何呢?是否有内置机制将IEnumerable < T > 绑定到下拉列表,然后在提交表单时自动保护正确的T?
手动完成它将是相当微不足道的 - 我只是试图确定我可以免费开箱即可.
给定一个类ThisClassShouldBeTheDataContext的实例作为视图的Datacontext
class ThisClassShouldBeTheDataContext
{
public Contacts Contacts {get;set;}
}
class Contacts
{
public IEnumerable<Person> Persons {get;set;}
public Person this[string Name]
{
get
{
var p = from i in Persons where i.Name = Name select i;
return p.First();
}
}
}
class Person
{
public string Name {get;set;}
public string PhoneNumber {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
如何绑定Contact["John"].PhoneNumber到文本框?
<TextBox Text="{Binding ?????}" />
Run Code Online (Sandbox Code Playgroud) 我有按钮:
<Button Content="Stop loading" />
Run Code Online (Sandbox Code Playgroud)
在ViewModel中,我有属性IsLoaded.我不想写属性IsNotLoaded但我想在IsLoaded = true时在绑定和禁用按钮中使用IsLoaded.
如何实现这样的事情:
<Button Content="Stop loading" IsEnabled="{Binding !IsLoaded}" />
Run Code Online (Sandbox Code Playgroud)
PS如果比编写附加属性更难,我将使用IsNotLoaded属性.
我正在使用应用程序的第二个版本,作为重写的一部分,我必须转向MVVM架构.我正面临压力,要在视图模型类中放置绝对的所有代码 - 在代码隐藏文件中使用c#是不受欢迎的.(我知道,我知道......我知道背后的代码不是坏事,但这次不是我的电话).
对于实现命令界面的对象,这很容易.我已经能够找到大量关于如何将这些对象的Command绑定到视图模型中的ICommand的信息.问题是没有这种接口的对象,例如
<ListBox
x:Name="myListBox"
MouseDoubleClick="myCallbackFunction">
<!-- ... -->
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我想知道如何将Listbox的MouseDoubleClick事件绑定到myCallbackFunction,这是在视图模型中实现的.这甚至可能吗?
谢谢!
我怎么能弄清楚xaml的哪一行包含麻烦的绑定当我的调试输出充满了如下所示的行:
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='UW.Entities.ProgramModel.UWProgram' BindingExpression:Path=; DataItem='RuntimeType' (HashCode=24995901); target element is 'DataGridCollectionViewSource' (HashCode=60976864); target property is 'Source' (type 'Object')
我不知道如何以一种可以让我找到负责任的xaml行的方式解释这个.我甚至无法弄清楚错误来自哪个xaml文件.在发生这些错误时,有什么方法可以获得更多信息吗?
'UW.Entities.ProgramModel.UWProgram'只是一种类型 - 我不知道被绑定的对象是什么.我在xaml的各个位中也有很多DataGridCollectionViewSources,所有属性'Source'都绑定到可能有或没有那种类型的东西(再次 - 没有简单的方法可以告诉).
我通过这种方式绑定到服务:
活动类:
ListenLocationService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, ListenLocationService.class);
intent.putExtra("From", "Main");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
...
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
}
public void onServiceDisconnected(ComponentName arg0) {
}
};
Run Code Online (Sandbox Code Playgroud)
这是onBind我服务的方法:
@Override
public IBinder onBind(Intent intent) {
Bundle extras = intent.getExtras();
if(extras == null)
Log.d("Service","null");
else
{
Log.d("Service","not null");
String from = …Run Code Online (Sandbox Code Playgroud) 如何将我的ViewModel(当前在BindingContext中)的ICommand对象绑定到XAML中ListView的ItemTapped或ItemSelected?
使用Button时这是一个简单的任务,我只需设置Command ="MyViewModelCommand",一切正常.
img在Angular2中设置标记的最佳方法是什么?
这不起作用(而不是因为键):
<img src="//maps.googleapis.com/maps/api/streetview?size=400x400&location="{{property.lat}}, {{property.long}}"key=aiwefmboij234blahblah" />
Run Code Online (Sandbox Code Playgroud)
我们如何将lat/long值传入src属性?
谢谢!
Groovy模板中的常规操作是将命名对象绑定到模板的范围,如下所示:
map.put("someObject",object)
template.bind(map)
Run Code Online (Sandbox Code Playgroud)
然后,在模板中我可以像这样引用并使用'someObject':
someObject.helloWorld()
someObject.helloWorld("Hi Everyone!")
someObject.helloWorld("Hi", "Everyone!")
Run Code Online (Sandbox Code Playgroud)
在模板内部,Groovy还允许我将方法句柄定义为模板中的第一类变量,如下所示:
someObject = someObject.&helloWorld
Run Code Online (Sandbox Code Playgroud)
然后,我可以在不参考对象和方法名称的情况下执行这些操作:
someObject()
someObject("Hello World")
someObject("Hello", "World")
Run Code Online (Sandbox Code Playgroud)
如何在'template.bind(map)'阶段绑定这样的方法引用以及自动解析脚本提供的所有参数组合,如'.&'运算符?
使用MethodClosure不起作用 - 这是一个简单的测试和我得到的错误
class TestMethodClass {
public void test() {
System.out.println("test()");
}
public void test(Object arg) {
System.out.println("test( " + arg + " )");
}
public void test(Object arg1, Object arg2) {
System.out.println("test( " + arg1 + ", " + arg2 + " )");
}
}
String basic = "<%" +
" def mc1=testInstance.&test;" +
"println \"mc1 class ${mc1.getClass()}\";" + …Run Code Online (Sandbox Code Playgroud)