我有一个自定义控件Workspace
,它继承Control
和在其中是一个DependencyProperty
我需要包含用户指定IEnumerable<IFoo>
(我也尝试使其非通用IEnumerable
).
Public Shared ReadOnly FoosProperty As DependencyProperty = DependencyProperty.Register("Foos", GetType(IEnumerable(Of IFoo)), GetType(Workspace), New FrameworkPropertyMetadata())
Public Property Foos() As IEnumerable(Of IFoo)
Get
Return CType(Me.GetValue(FoosProperty), IEnumerable(Of IFoo))
End Get
Set(ByVal value As IEnumerable(Of IFoo))
Me.SetValue(FoosProperty, CType(value, IEnumerable(Of IFoo)))
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
当我创建并设置一个IFoo
代码数组时,一切都很完美,但是当我尝试在XAML中添加它们时,我得到了错误.如果我添加一个IFoo
我得到错误
在运行时.如果我尝试添加多个IFoo
项目,我会在编译时遇到三个错误
我读错误意味着WPF没有像往常那样将xaml转换为项目数组.以下是我尝试在XAML中添加项目的方法
<Workspace>
<Workspace.Foos>
<FooItem />
<FooItem />
</Workspace.Foos>
</Workspace>
Run Code Online (Sandbox Code Playgroud)
我在过去创建了类似的DependencyProperties,从来没有遇到过问题所以我猜我错过了一些简单的东西.
谢谢你的帮助!
我有很多现有的业务对象,其中包含许多属性和集合,我想在其中绑定用户接口.使用DependencyProperty
或ObservableCollections
在这些对象中使用不是一种选择.据我所知,当我修改这些对象时,我希望有一种机制来在我执行此操作时更新所有UI控件.另外,我也不知道哪些UI控件绑定到这些对象以及哪些属性.
这是我现在尝试做的简化代码:
public class Artikel
{
public int MyProperty {get;set;}
}
public partial class MainWindow : Window
{
public Artikel artikel
{
get { return (Artikel)GetValue(artikelProperty); }
set { SetValue(artikelProperty, value); }
}
public static readonly DependencyProperty artikelProperty =
DependencyProperty.Register("artikel", typeof(Artikel), typeof(MainWindow), new UIPropertyMetadata(new Artikel()));
public MainWindow()
{
InitializeComponent();
test.DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
artikel.MyProperty += 1;
// What can I do at this point to update all bindings?
// …
Run Code Online (Sandbox Code Playgroud) 我正在使用 MVVM 模式在 WPF/c# 中编写表单并尝试与用户控件共享数据。(好吧,用户控件视图模型)
我要么需要:
用户控件仅用于使大型表单更易于管理,所以我不确定这是否是 MVVM 的方式,这正是我过去所做的方式。
我想在 Xaml 中传递一个 VM 构造的类。
<TabItem Header="Applicants">
<Views:ApplicantTabView>
<UserControl.DataContext>
<ViewModels:ApplicantTabViewModel Client="{Binding Client} />
</UserControl.DataContext>
</Views:ApplicantTabView>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
<TabItem Header="Applicants">
<Views:ApplicantTabView>
<UserControl.DataContext>
<ViewModels:ApplicantTabViewModel Client="{Binding Client} />
</UserControl.DataContext>
</Views:ApplicantTabView>
</TabItem>
Run Code Online (Sandbox Code Playgroud)
但我似乎无法获得接受非静态内容的依赖属性。
这对我来说已经有一段时间了,但假设我会发现但失败了,所以我在这里。
提前致谢,奥利
我使用下面的代码来显示WPF(MVVM)应用程序中的进度条状态,该应用程序基本上是在一个线程上,但是如果我在调度程序线程上放置它的状态没有在UI上更新.
有线程:
Main()
{
Thread LoadApp = new Thread(MyData);
LoadApp.Start();
}
public void MyData()
{
ProgMessage = "10%";
ProgValue = 10;
var varAction = new List<Func<object>>();
varAction .Add(() => (MainViewMode1)ViewModel1.ViewModel1);
varAction .Add(() => (MainViewMode2)ViewModel2.ViewModel2);
varAction .Add(() => (MainViewMode3)ViewModel3.ViewModel3);
foreach (var action in varAction )
{
var obj = action();
ProgressMessage = // My message from VM
ProgressBarValue += // My message valuefrom VM;
}
Run Code Online (Sandbox Code Playgroud)
}
这工作正常但是从现在开始我使用依赖属性我得到了交叉线程异常(STA)所以我将代码更改为:
Main()
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
{
MyData();
}));
}
public void MyData()
{ …
Run Code Online (Sandbox Code Playgroud) 我想要两个共享一个DepedencyProperty
类使用AddOwner
(欢迎任何其他方法),例如
class ClassA : DependencyObject
{
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int), typeof(ClassA),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
}
Run Code Online (Sandbox Code Playgroud)
和
class ClassB : DependencyObject
{
public int Number
{
get { return (int)GetValue(NumberProperty); }
set { SetValue(NumberProperty, value); }
}
public static readonly DependencyProperty NumberProperty =
ClassA.NumberProperty.AddOwner(typeof(ClassB),
new FrameworkPropertyMetadata(0,
FrameworkPropertyMetadataOptions.Inherits));
}
Run Code Online (Sandbox Code Playgroud)
就像这里描述的那样。正如您可能猜到的那样:当然它不起作用。这是完全有道理的,因为不可能创建同一个类的多个实例,而这些实例都具有“自己的”依赖属性。
我如何确保所有类(特别是所有实例)的ClassA
, …
我刚刚发现自己可以做到以下几点:
var button = new Button();
button.SetValue(TextBlock.TextProperty, "text");
var text = (string)button.GetValue(TextBlock.TextProperty); // text is "text"
Run Code Online (Sandbox Code Playgroud)
尽管上面的示例有些不切实际,但它确实表明我可以将常规依赖项属性附加到另一个对象上。它不必是附加的属性(TextBlock.TextProperty
未在中注册)DependencyProperty.RegisterAttached()
。
这就提出了一个问题,为什么首先要有附加属性?我现在看到的唯一区别是,我无法在XAML中附加常规依赖项属性。就是这样。还有其他区别吗?
更新:
为了更加清楚,下面的代码可以正常工作,并且从最终用户的角度来看,它看起来非常接近附加属性:
public static class AttachedPropertyDeclarer
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text",
typeof(string),
typeof(Button),
new PropertyMetadata(default(string),OnTextChanged));
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// do something when text changed
}
}
...
button.SetValue(AttachedPropertyDeclarer.TextProperty, "text");
var text = (string)button.GetValue(AttachedPropertyDeclarer.TextProperty);
Run Code Online (Sandbox Code Playgroud)
将此与附加的属性方式进行比较:
public static class AttachedPropertyDeclarer
{
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text",
typeof(string),
typeof(AttachedPropertyDeclarer), …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在依赖项值更改时更新视图。
我已将代码从视图复制到其父级中,并且没有使用依赖项,并且工作正常。我相信我的问题在于如何创建 DependencyProperty。
public partial class CULabelConfigControl : UserControl {
private CreditUnion CU { get; set; }
public static readonly DependencyProperty CUProperty = DependencyProperty.Register(
"CU",
typeof(CreditUnion),
typeof(CULabelConfigControl),
new FrameworkPropertyMetadata(null)
);
Run Code Online (Sandbox Code Playgroud)
我目前在运行时收到错误:
"A 'Binding' cannot be set on the 'CU' property of type 'CULabelConfigControl'.
A 'Binding' can only be set on a DependencyProperty of a DependencyObject."
Run Code Online (Sandbox Code Playgroud)
任何方向正确的点都会有所帮助。如果我需要分享任何其他细节,请告诉我。
我做什么
创建一个用户控件,其中一个DependencyProperty绑定到一个字符串数组
public static readonly DependencyProperty TaskListProperty =
DependencyProperty.Register("TaskList",
typeof(string[]),
typeof(MainControl),
null);
public string TaskList
{
get { return (string[])GetValue(TaskListProperty); }
set { SetValue(TaskListProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
我
为此代码收到了什么错误
get { return (string[])GetValue(TaskListProperty); }
Run Code Online (Sandbox Code Playgroud)
错误:无法将类型'string []'隐式转换为'string'
如何
为什么这个错误发生,不要我注册其源类型为字符串数组?怎么解决?
c# data-binding silverlight dependency-properties windows-phone-7
wpf ×7
c# ×5
data-binding ×2
xaml ×2
binding ×1
dependencies ×1
dispatcher ×1
ienumerable ×1
mvvm ×1
silverlight ×1