在我的应用程序中我使用的是usercontrol
称为“ChannelControls”,我在主窗口上实例了 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
字符串、双精度等...)。这ChannelControls
will 包含许多滑块、按钮和其他用户控件,但目前我正在尝试仅绑定其中之一。
这是这个自定义类的一部分,它将保存数据,它有一个私有构造函数,因为我需要从任何地方访问它:
[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");
}
}
}
这是我尝试绑定通道控制属性的方法ChannelSpriteCount
到我的单例类:cmixdata。
<CMiX:ChannelControls x:Name="Layer0" Tag="0" Visibility="Visible" ChannelSpriteCount="{Binding SpriteCount[0], Mode=TwoWay}"/>
在主用户控件上,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}"
但由于某种原因,cmixdata 中的属性没有更新,并且始终具有默认值......