开发者

creating a customer user control in wpf and synchronizing with selected item

OK - So I almost have this working. I just need to know who to get the usercontrol to let the viewmodel of the consuming view know there has been a change. Check this out - here is xaml from the consuming view.

  <StackPanel>
    <pfControls:TriChoiceUserControl Text="{Binding Path=SampleText}" State="{Binding CurrentState}"/>
    </StackPanel>

Here is the viewmodel code

class MainWindowViewModel: INotifyPropertyChanged

{ private bool? currentState; public bool? CurrentS开发者_JAVA技巧tate { get { return currentState; } set { currentState = value; OnPropertyChanged("CurrentState"); } }

public string SampleText  { get { return "Hi there"; }  }

public MainWindowViewModel()
{
  CurrentState = false;
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
  PropertyChangedEventHandler handler = PropertyChanged;
  if (handler != null)
  {
    handler(this, new PropertyChangedEventArgs(name));
  }
}

}

Now on the initial load of the ViewModel you can see that Current state is false and indeed the control I ends up with the false check box checked (there are three check boxes, one for yes, one for no and one for na - don't ask me, that is what they told me to do). Problem is that when I check the first one (true in this case) the user control is working in that it goes and unchecks the false check box but and changes the state property but my viewmodel for the consuming view never gets notified. I feel like I am so close... Here is the code for the user control.

 public partial class TriChoiceUserControl : UserControl, INotifyPropertyChanged

{ #region Fields (5) 

public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(bool?), typeof(TriChoiceUserControl), 
  new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangeState)));
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(TriChoiceUserControl), 
  new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangeText)));

    #endregion Fields 


public TriChoiceUserControl()
{
  InitializeComponent();
}


public bool? State
{
  get
  {
    return (bool?)GetValue(StateProperty);
  }
  set
  {
    SetValue(StateProperty, value);
    NotifyPropertyChanged("State");
  }
}

public string Text
{
  get
  {
    return (string)GetValue(TextProperty);
  }
  set
  {
    SetValue(TextProperty, value);
  }
}

private static void ChangeState(DependencyObject source, DependencyPropertyChangedEventArgs e)
{

  (source as TriChoiceUserControl).UpdateCheckState((bool?)e.NewValue);
}

private static void ChangeText(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
  (source as TriChoiceUserControl).UpdateText(e.NewValue.ToString());
}

private void UpdateText(string newText)
{
  label1.Content = newText;
}

private void UpdateCheckState(bool? newState)
{
  if (newState != null)
  {
    if ((bool)newState)
    {
      chkYes.IsChecked = true;
      chkNo.IsChecked = false;
      chkNa.IsChecked = false;
    }
    else
    {
      chkYes.IsChecked = false;
      chkNo.IsChecked = true;
      chkNa.IsChecked = false;
    }
  } 
  else
  {
    chkYes.IsChecked = false;
    chkNo.IsChecked = false;
    chkNa.IsChecked = true;
  }
  State = newState;
}


private void chkYes_Checked(object sender, RoutedEventArgs e)
{
  UpdateCheckState(true);
}

private void chkNo_Checked(object sender, RoutedEventArgs e)
{
  UpdateCheckState(false);
}

private void chkNa_Checked(object sender, RoutedEventArgs e)
{
  UpdateCheckState(null);
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
  if (PropertyChanged != null)
  {
    PropertyChanged(this, new PropertyChangedEventArgs(info));
  }
}

}

Here is the XAML for the user control.

Thanks for any input.


All of this works just fine, I had lost sight of the fact that the default mode is "oneWay" on the binding - duh - I set Mode=TwoWay and no everything works. But that OK, I don't mind saying duh, it usually means I have found the answer :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜