[英]WPF: UserControl with Dependency Property and sub-controls
I'm trying to create my own, very simple, Usercontrol in WPF. 我试图在WPF中创建自己的非常简单的Usercontrol。 It's basically just a Combobox, with some additional Logic in it. 它基本上只是一个组合框,其中包含一些其他逻辑。 I tried to create my own Depdency-Property using this Tutorial: http://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property 我尝试使用此教程创建自己的Depdency-Property: http : //www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property
This is working fine so far, but if the property changes, I'd like to reflect this on the Combobox in the User-Control as well. 到目前为止,此方法运行良好,但是如果属性发生更改,我也想在用户控件的组合框中反映出来。 It seems like I can't bind the subcontrol directly to my new Dependency-Project. 看来我无法将子控件直接绑定到新的Dependency-Project。
My code is looking like this at the moment: 我的代码目前看起来像这样:
public partial class ClassSelector : UserControl
{
public static readonly DependencyProperty CurrentValueProperty =
DependencyProperty.Register("CurrentValue", typeof(ClassType),
typeof(ClassSelector), new FrameworkPropertyMetadata());
public ClassType CurrentValue
{
get
{
return (ClassType)this.GetValue(CurrentValueProperty);
}
set
{
this.SetValue(CurrentValueProperty, value);
}
}
public ClassSelector()
{
this.DataContext = this;
InitializeComponent();
cmbClassType.ItemsSource = Enum.GetValues(typeof(ClassType));
}
}
Setting the value of the dependy-property or the Combobox seems weirds to me. 设置依赖属性或组合框的值对我来说很奇怪。 I tried to bind it direclty in the xaml via: 我试图通过以下方式将其直接绑定到xaml中:
<Grid>
<ComboBox x:Name="cmbClassType" SelectedItem="{Binding Path=CurrentValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="cmbClassType_SelectionChanged" />
</Grid>
I tried to map the Dependicy-Project changed Event with the combobox and visa versa, but this leads to very strange code, since the combobox change would have to change the property-value and the property-value the combobox. 我试图用组合框来映射Dependicy-Project更改的事件,反之亦然,但这会导致非常奇怪的代码,因为组合框的更改将不得不更改组合框的属性值和属性值。
I'm quite sure there has to be a possibility to bind a DependencyProperty to a subcontrol, but I can't find a way to make this work. 我非常确定必须有可能将DependencyProperty绑定到子控件,但是我找不到实现此工作的方法。
Thanks in advance for all advices guys and have a nice weekend 在此先感谢您提供的所有建议,并祝您周末愉快
Matthias 马蒂亚斯
Edith says: The calling Window needs to bind the Object to the Grid, not to the Window, so for example: Edith说:调用窗口需要将对象绑定到网格,而不是窗口,因此,例如:
grdMain.DataContext = new DeckSearch();
is working fine, meanwhile 同时工作正常
this.DataContext = new DeckSearch();
This behavior is ONLY at my custom control, all other controls worked perfectly fine with the DataContext on the Window itself. 此行为仅在我的自定义控件上有效,所有其他控件与Window本身上的DataContext完美配合。
Okay so here I fixed your code and it is working at my end 好的,所以我在这里修复了您的代码,该代码终于可以正常工作了
UserControlCodeBehind UserControlCodeBehind
public partial class ClassSelector : UserControl
{
public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(ClassType),
typeof(ClassSelector), new FrameworkPropertyMetadata()
{
DefaultValue = ClassType.Type1,
BindsTwoWayByDefault = true,
PropertyChangedCallback = CurrentValueChanged,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
private static void CurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ClassSelector)d;
obj.cmbClassType.SelectedValue = e.NewValue;
}
public ClassType CurrentValue
{
get
{
return (ClassType)this.GetValue(CurrentValueProperty);
}
set
{
this.SetValue(CurrentValueProperty, value);
}
}
public ClassSelector()
{
InitializeComponent();
cmbClassType.ItemsSource = Enum.GetValues(typeof(ClassType));
cmbClassType.SelectedValue = CurrentValue;
}
}
The Xaml part of the UserControl UserControl的Xaml部分
<Grid>
<ComboBox x:Name="cmbClassType" SelectedValue="{Binding Path=CurrentValue}"/>
</Grid>
Please check if it is working at your end. 请检查它是否在您的终端正常工作。 I have not added any extra code checking for thread safety and all. 我没有添加任何额外的代码检查线程安全等等。
EDIT 编辑
In my solution I do get my Class
Property
notification when the CurrentValue
changes. 在解决方案中,当CurrentValue
更改时,我的确收到了Class
Property
通知。
Below is my sample MainWindow Code. 下面是我的示例MainWindow代码。
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Task.Run(() =>
{
Thread.Sleep(1000);
Dispatcher.InvokeAsync(() =>
{
customCombobox.CurrentValue = ClassType.Type3;//Updating the UserControl DP
});
Thread.Sleep(2000);
this.CurrentValue = ClassType.Type2;//Updating my local Property
});
}
private ClassType _currentValue;
public ClassType CurrentValue
{
get { return _currentValue; }
set
{
_currentValue = value;
Debug.WriteLine("Value Changed to " + value.ToString());
RaisePropertyChanged("CurrentValue");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string propName)
{
var pc = PropertyChanged;
if (pc != null)
pc(this, new PropertyChangedEventArgs(propName));
}
}
And my MainWindow.xaml 还有我的MainWindow.xaml
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication"
Title="MainWindow" Height="250" Width="525">
<local:ClassSelector x:Name="customCombobox" Height="25" CurrentValue="{Binding CurrentValue}"/>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.