C# 将usercontrol属性绑定到自定义类
在我的应用程序中,我使用了一个名为“ChannelControls”的C# 将usercontrol属性绑定到自定义类,c#,wpf,C#,Wpf,在我的应用程序中,我使用了一个名为“ChannelControls”的usercontrol,我在主窗口上执行了6次 public partial class ChannelControls : UserControl { CMiXData cmixdata = CMiXData.Instance; public ChannelControls() { InitializeComponent(); this.DataContext = th
usercontrol
,我在主窗口上执行了6次
public partial class ChannelControls : UserControl
{
CMiXData cmixdata = CMiXData.Instance;
public ChannelControls()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty ChannelSpriteCountProperty =
DependencyProperty.Register("ChannelSpriteCount", typeof(string), typeof(ChannelControls), new PropertyMetadata("1"));
[Bindable(true)]
public string ChannelSpriteCount
{
get { return (string)this.GetValue(ChannelSpriteCountProperty); }
set { this.SetValue(ChannelSpriteCountProperty, value); }
}
我正在使用一个名为cmixdata的自定义类来保存我的应用程序的所有数据(它将包含不同的属性,包括List
of string、double等)。ChannelControls
将包含许多滑块、按钮和其他用户控件,但目前我正试图绑定其中一个
这里是这个自定义类的一部分,它将保存数据,它有一个私有构造函数,因为我需要从任何地方访问它:
[Serializable]
public class CMiXData : INotifyPropertyChanged
{
private static CMiXData _instance = null;
public static CMiXData Instance
{
get
{
if (_instance == null)
{
_instance = new CMiXData();
}
return _instance;
}
}
private CMiXData() { } //prevent instantiation from outside the class
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
MessageBox.Show(propertyName);
}
private List<string> _SpriteCount = new List<string>(new string[] {"1", "1", "1", "1", "1", "1"});
public List<string> SpriteCount
{
get { return _SpriteCount; }
set
{
if(_SpriteCount != value)
{
_SpriteCount = value;
RaisePropertyChanged("SpriteCount");
}
}
}
在主用户控件上,实例化了ChannelControls
,datacontext
的设置方式如下:
public partial class CMiX_UI : UserControl
{
BeatSystem beatsystem = new BeatSystem();
CMiXData cmixdata = CMiXData.Instance;
public CMiX_UI()
{
InitializeComponent();
this.DataContext = cmixdata;
}
在xaml方面:
<UserControl
x:Class="CMiX.CMiX_UI"
DataContext="{x:Static CMiX:CMiXData.Instance}"
用户控件永远不应该拥有视图模型的“自己”实例。相反,它应该具有绑定到“外部”视图模型的属性的依赖项属性
您的ChannelsControl将声明如下属性(其中我假设string
不是适合计数的类型):
在ChannelsControl的XAML中,您可以这样绑定它:
<CMiX:Counter Count="{Binding SpriteCount,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<Window.DataContext>
<local:CMiXData />
</Window.DataContext>
...
<local:ChannelsControl SpriteCount="{Binding SpriteCount[0]}" ... />
<ItemsControl ItemsSource="{Binding SpriteCount}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ChannelsControl SpriteCount="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Window ... DataContext="{x:Static local:CMiXData.Instance}" >
CMiX:Counter
和ChannelControls
应该是同一个UserControl类吗?CMiX:Counter在ChannelControls中。如果UpdateSourceTrigger=PropertyChanged
没有意义,绑定应该可以工作,因为您已经将ChannelControls的DataContext设置为CMiXData的实例,并且该DataContext值应由子元素继承。在调用InitializeComponent之前,您可以尝试设置DataContext。但是,通常情况下,您不应该显式设置UserControl的DataContext,原因如下:或者。什么类型是Counter.Count?是的,我尝试过,但是ChannelControl会重复6次,并且不会绑定到第一个元素。ChannelControls 1将绑定到第一个,ChannelControls 2将绑定到第二个,依此类推…我理解你的答案,谢谢你,但如果我想动态创建更多的ChannelControls实例,我别无选择,只能在代码后面编写所有绑定,对吗?抱歉,今天刚刚检查,它仍然不起作用,我无法在XAML中声明datacontext,因为构造函数是私有的。我需要这个类在任何地方都可以访问,所以它有一个私有构造函数。谢谢你的支持,但肯定有一个细节我在哪里遗漏了,我已经更新了我的帖子。
<ItemsControl ItemsSource="{Binding SpriteCount}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ChannelsControl SpriteCount="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Window ... DataContext="{x:Static local:CMiXData.Instance}" >
DataContext = CMiXData.Instance;