User Control Question
-
This is a bit long, and I'm not entirely sure how to word my question... I am developing a prototype for a simple lookup control[^]. The source is here[^]. The Category combo is on the window, and below it is my control. When a category is selected, I want the Lookup control to get the category Id:
My Lookup control's VM is pretty simple:
public class LookupComboControlViewModel : _ModelBase
{
private List _People = new List();
public List People
{
get { return _People; }
set
{
if (_People != value)
{
_People = value;
RaisePropertyChanged("ComboSourceData");
}
}
}private string \_CategoryName; public string CategoryName { get { return \_CategoryName; } set { if (\_CategoryName != value) { \_CategoryName = value; RaisePropertyChanged("CategoryName"); } } } private int \_CategoryId; public int CategoryId { get { return \_CategoryId; } set { if (\_CategoryId != value) { \_CategoryId = value; RaisePropertyChanged("CategoryId"); loadData(); } } } private v
-
This is a bit long, and I'm not entirely sure how to word my question... I am developing a prototype for a simple lookup control[^]. The source is here[^]. The Category combo is on the window, and below it is my control. When a category is selected, I want the Lookup control to get the category Id:
My Lookup control's VM is pretty simple:
public class LookupComboControlViewModel : _ModelBase
{
private List _People = new List();
public List People
{
get { return _People; }
set
{
if (_People != value)
{
_People = value;
RaisePropertyChanged("ComboSourceData");
}
}
}private string \_CategoryName; public string CategoryName { get { return \_CategoryName; } set { if (\_CategoryName != value) { \_CategoryName = value; RaisePropertyChanged("CategoryName"); } } } private int \_CategoryId; public int CategoryId { get { return \_CategoryId; } set { if (\_CategoryId != value) { \_CategoryId = value; RaisePropertyChanged("CategoryId"); loadData(); } } } private v
-
Don't you need to bind the SelectedItem property in the Combo to something so you know what is selected?
I could be wrong, but I don't think so. The user control is bound to the Category combo's SelectedValue, so when a category is selected, it's value (CategoryId), should be passed to my control.
views:LookupComboControl Grid.Row="2"
CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
Height="70"
Width="400"/>If it's not broken, fix it until it is
-
I could be wrong, but I don't think so. The user control is bound to the Category combo's SelectedValue, so when a category is selected, it's value (CategoryId), should be passed to my control.
views:LookupComboControl Grid.Row="2"
CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
Height="70"
Width="400"/>If it's not broken, fix it until it is
I think you are right in principal, but in practice I'm sure I've seen/had problems with selected value not working as expected... For the sake of a test, change it to bind to the text property and see if it starts working- at least that narrows the issue down!
-
I think you are right in principal, but in practice I'm sure I've seen/had problems with selected value not working as expected... For the sake of a test, change it to bind to the text property and see if it starts working- at least that narrows the issue down!
OK, I think you were right. The Output window was showing a Null being passed as SelectedValue. So I made the following changes:: In the control:
public object SelectecCategory
{
get { return (object)GetValue(SelectecCategoryProperty); }
set
{
SetValue(SelectecCategoryProperty, value);
}
}public static readonly DependencyProperty SelectecCategoryProperty =
DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl));In the MainWindow
Now I get no binding errors. But the original problem still exists... Nothing happens. So here's my question - The UserControl has the DP. Once the MainWindow sets the DP, how does the User Control's VM know about it? Again, I need to somehow get the selected category into the ViewModel. [UPDATE] The source is https://onedrive.live.com/redir?resid=F6FBCF1880A16630!210&authkey=!AA4CNT499dsekPY&ithint=file%2c.zip
If it's not broken, fix it until it is
-
OK, I think you were right. The Output window was showing a Null being passed as SelectedValue. So I made the following changes:: In the control:
public object SelectecCategory
{
get { return (object)GetValue(SelectecCategoryProperty); }
set
{
SetValue(SelectecCategoryProperty, value);
}
}public static readonly DependencyProperty SelectecCategoryProperty =
DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl));In the MainWindow
Now I get no binding errors. But the original problem still exists... Nothing happens. So here's my question - The UserControl has the DP. Once the MainWindow sets the DP, how does the User Control's VM know about it? Again, I need to somehow get the selected category into the ViewModel. [UPDATE] The source is https://onedrive.live.com/redir?resid=F6FBCF1880A16630!210&authkey=!AA4CNT499dsekPY&ithint=file%2c.zip
If it's not broken, fix it until it is
Are you still stuck on this? I realise it's a bit late, but anyhow... Assuming your binding is working (didn't test your code, can't at the moment), all you would need to do is add a callback for the dependency property change.
public static readonly DependencyProperty SelectecCategoryProperty =
DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl), new UIPropertyMetadata(new PropertyChangedCallback(OnChanged))););...
private static void OnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
//obj is your control. you can cast it here and call a method on it to get rid of the static state you are in
var control = obj as LookupComboControl;
control.SetCategoryOnVm();
}private void SetCategoryOnVm()
{
vm.Category = this.SelectedCategory //(= your dependency property)
}