//Метод, убирающий красные рамки с текстбоксов private void tb_GotFocus(object sender, RoutedEventArgs e) { if (sender is PasswordBox) { PasswordBox text_box = (PasswordBox)sender; text_box.ClearValue(BorderBrushProperty); text_box.BorderThickness = new Thickness(1); foreach (object child in grid_layout.Children) { if (!(child is Label)) { continue; } if (((Label)child).Tag != null && text_box.Tag != null && ((Label)child).Tag.ToString() == text_box.Tag.ToString()) { ((Label)child).Visibility = Visibility.Hidden; } } } else { TextBox text_box = (TextBox)sender; text_box.ClearValue(BorderBrushProperty); text_box.BorderThickness = new Thickness(1); foreach (object child in grid_layout.Children) { if (!(child is Label)) { continue; } if (((Label)child).Tag != null && text_box.Tag != null && ((Label)child).Tag.ToString() == text_box.Tag.ToString()) { ((Label)child).Visibility = Visibility.Hidden; } } } }
public void SetText2 () { TextBox box = new TextBox { Text = "Blah" }; Assert.AreEqual ("Blah", box.GetValue (TextBox.TextProperty), "#1"); box.SetValue (TextBox.TextProperty, null); Assert.AreEqual ("", box.GetValue (TextBox.TextProperty), "#2"); box.Text = "o hi"; box.ClearValue (TextBox.TextProperty); Assert.AreEqual ("", box.GetValue (TextBox.TextProperty), "#3"); }
public void ClearingTheAttachedPropertyRemovesTheValidationRule() { var textBox = new TextBox(); textBox.BeginInit(); var binding = new Binding("ValidatedStringProperty") { Mode = BindingMode.OneWayToSource, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding); textBox.EndInit(); Validate.SetBindingForProperty(textBox, "Text"); Assert.AreEqual(1, binding.ValidationRules.OfType<ValidatorRule>().Count()); textBox.ClearValue(Validate.BindingForPropertyProperty); Assert.AreEqual(0, binding.ValidationRules.OfType<ValidatorRule>().Count()); }
private void RenderEditItemField(Item item, Field field) { // skip rendering a hidden field if (field.DisplayType == DisplayTypes.Hidden) return; PropertyInfo pi = null; object currentValue = null; object container = null; // get the current field value. // the value can either be in a strongly-typed property on the item (e.g. Name), // or in one of the FieldValues try { // get the strongly typed property pi = item.GetType().GetProperty(field.Name); if (pi != null) { // store current item's value for this field currentValue = pi.GetValue(item, null); // set the container - this will be the object that will be passed // to pi.SetValue() below to poke new values into container = itemCopy; } } catch (Exception) { // an exception indicates this isn't a strongly typed property on the Item // this is NOT an error condition } // if couldn't find a strongly typed property, this property is stored as a // FieldValue on the item if (pi == null) { // get current item's value for this field, or create a new FieldValue // if one doesn't already exist FieldValue fieldValue = item.GetFieldValue(field.ID, true); currentValue = fieldValue.Value; // get the value property of the current fieldvalue (this should never fail) pi = fieldValue.GetType().GetProperty("Value"); if (pi == null) return; // set the container - this will be the object that will be passed // to pi.SetValue() below to poke new values into container = fieldValue; } ListBoxItem listBoxItem = new ListBoxItem(); StackPanel EditStackPanel = new StackPanel(); listBoxItem.Content = EditStackPanel; EditStackPanel.Children.Add( new TextBlock() { Text = field.DisplayName, Style = (Style)App.Current.Resources["PhoneTextNormalStyle"] }); // create a textbox (will be used by the majority of field types) double minWidth = App.Current.RootVisual.RenderSize.Width; if ((int)minWidth == 0) minWidth = ((this.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait) ? 480.0 : 800.0; TextBox tb = new TextBox() { DataContext = container, MinWidth = minWidth, IsTabStop = true }; tb.SetBinding(TextBox.TextProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); bool notMatched = false; // render the right control based on the DisplayType switch (field.DisplayType) { case DisplayTypes.Text: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Text } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); EditStackPanel.Children.Add(tb); break; case DisplayTypes.TextArea: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Text } } }; tb.AcceptsReturn = true; tb.TextWrapping = TextWrapping.Wrap; tb.Height = 150; tb.TabIndex = tabIndex++; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); EditStackPanel.Children.Add(tb); break; case DisplayTypes.Phone: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.TelephoneNumber } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.MinWidth -= 64; tb.MaxWidth = tb.MinWidth; StackPanel innerPanel = RenderImageButtonPanel(tb); ImageButton imageButton = (ImageButton)innerPanel.Children[1]; imageButton.Click += new RoutedEventHandler(delegate { PhoneNumberChooserTask chooser = new PhoneNumberChooserTask(); chooser.Completed += new EventHandler<PhoneNumberResult>((sender, e) => { if (e.TaskResult == TaskResult.OK && e.PhoneNumber != null && e.PhoneNumber != "") pi.SetValue(container, e.PhoneNumber, null); }); chooser.Show(); }); EditStackPanel.Children.Add(innerPanel); break; case DisplayTypes.Link: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Url } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); EditStackPanel.Children.Add(tb); break; case DisplayTypes.Email: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.EmailSmtpAddress } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); tb.MinWidth -= 64; tb.MaxWidth = tb.MinWidth; innerPanel = RenderImageButtonPanel(tb); imageButton = (ImageButton)innerPanel.Children[1]; imageButton.Click += new RoutedEventHandler(delegate { EmailAddressChooserTask chooser = new EmailAddressChooserTask(); chooser.Completed += new EventHandler<EmailResult>((s, e) => { if (e.TaskResult == TaskResult.OK && !String.IsNullOrEmpty(e.Email)) { pi.SetValue(container, e.Email, null); // find the contact using the email address Contacts contacts = new Contacts(); contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>((sen, ev) => { // save the contact info as a new contact var contact = ev.Results.FirstOrDefault(); if (contact == null) return; ContactPickerHelper.AddContactInfo(contact, item); }); contacts.SearchAsync(e.Email, FilterKind.EmailAddress, null); } }); chooser.Show(); }); EditStackPanel.Children.Add(innerPanel); break; case DisplayTypes.Address: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.AddressStreet }, new InputScopeName() { NameValue = InputScopeNameValue.AddressCity }, new InputScopeName() { NameValue = InputScopeNameValue.AddressStateOrProvince }, new InputScopeName() { NameValue = InputScopeNameValue.AddressCountryName }, } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); tb.MinWidth -= 64; tb.MaxWidth = tb.MinWidth; innerPanel = RenderImageButtonPanel(tb); imageButton = (ImageButton)innerPanel.Children[1]; imageButton.Click += new RoutedEventHandler(delegate { // start the location service GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); watcher.MovementThreshold = 20; // Use MovementThreshold to ignore noise in the signal. watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>((sender, e) => { if (e.Status == GeoPositionStatus.Ready) { // Use the Position property of the GeoCoordinateWatcher object to get the current location. GeoCoordinate co = watcher.Position.Location; tb.Text = co.Latitude.ToString("0.000") + "," + co.Longitude.ToString("0.000"); // Stop the Location Service to conserve battery power. watcher.Stop(); // also store the latlong information in a hidden LatLong FieldValue var latlong = item.GetFieldValue(FieldNames.LatLong, true); if (latlong != null) latlong.Value = tb.Text; } }); watcher.Start(); }); EditStackPanel.Children.Add(innerPanel); break; case DisplayTypes.Priority: ListPicker lp = new ListPicker() { MinWidth = minWidth, FullModeItemTemplate = (DataTemplate)App.Current.Resources["FullListPickerTemplate"], IsTabStop = true }; lp.ItemsSource = App.ViewModel.Constants.Priorities; lp.DisplayMemberPath = "Name"; int? lpval = (int?)pi.GetValue(container, null); if (lpval != null) lp.SelectedIndex = (int)lpval; else lp.SelectedIndex = 1; // HACK: hardcode to "Normal" priority. this should come from a table. lp.SelectionChanged += new SelectionChangedEventHandler(delegate { pi.SetValue(container, lp.SelectedIndex == 1 ? (int?)null : lp.SelectedIndex, null); }); lp.TabIndex = tabIndex++; EditStackPanel.Children.Add(lp); break; case DisplayTypes.Folders: ListPicker folderPicker = new ListPicker() { MinWidth = minWidth, IsTabStop = true }; folderPicker.ItemsSource = App.ViewModel.Folders; folderPicker.DisplayMemberPath = "Name"; Folder tl = App.ViewModel.Folders.FirstOrDefault(list => list.ID == folder.ID); folderPicker.SelectedIndex = App.ViewModel.Folders.IndexOf(tl); folderPicker.SelectionChanged += new SelectionChangedEventHandler(delegate { pi.SetValue(container, App.ViewModel.Folders[folderPicker.SelectedIndex].ID, null); }); folderPicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(folderPicker); break; case DisplayTypes.Lists: ListPicker listPicker = new ListPicker() { MinWidth = minWidth, FullModeItemTemplate = (DataTemplate)App.Current.Resources["FullListPickerTemplate"], ExpansionMode = ExpansionMode.FullScreenOnly, IsTabStop = true }; var lists = App.ViewModel.Items. Where(i => i.FolderID == item.FolderID && i.IsList == true && i.ItemTypeID != SystemItemTypes.Reference). OrderBy(i => i.Name). ToObservableCollection(); lists.Insert(0, new Item() { ID = Guid.Empty, Name = folder.Name + " (folder)" }); listPicker.ItemsSource = lists; listPicker.DisplayMemberPath = "Name"; var listGuid = currentValue != null ? (Guid) currentValue : Guid.Empty; Item thisItem = lists.FirstOrDefault(i => i.ID == (Guid) listGuid); // if the list isn't found (e.g. ParentID == null), SelectedIndex will default to the Folder scope (which is correct for that case) listPicker.SelectedIndex = Math.Max(lists.IndexOf(thisItem), 0); listPicker.SelectionChanged += new SelectionChangedEventHandler(delegate { pi.SetValue(container, lists[listPicker.SelectedIndex].ID, null); }); listPicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(listPicker); break; case DisplayTypes.DatePicker: DatePicker dp = new DatePicker() { DataContext = container, MinWidth = minWidth, IsTabStop = true }; DateTime date = Convert.ToDateTime((string)currentValue); if (date.Ticks == 0) date = DateTime.Now.Date; dp.Value = date; dp.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(delegate { //pi.SetValue(container, dp.Value, null); pi.SetValue(container, dp.Value == null ? null : ((DateTime)dp.Value).ToString("d"), null); folder.NotifyPropertyChanged("FirstDue"); folder.NotifyPropertyChanged("FirstDueColor"); }); dp.TabIndex = tabIndex++; EditStackPanel.Children.Add(dp); break; case DisplayTypes.DateTimePicker: // create date picker DatePicker datePicker = new DatePicker() { DataContext = container, MinWidth = minWidth, IsTabStop = true }; // set up two-way data binding so that we don't have to pick up the new value in the event handler datePicker.SetBinding(DatePicker.ValueProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(delegate { folder.NotifyPropertyChanged("FirstDue"); folder.NotifyPropertyChanged("FirstDueColor"); }); datePicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(datePicker); // create time picker TimePicker timePicker = new TimePicker() { DataContext = container, MinWidth = minWidth, IsTabStop = true }; // set up two-way data binding so that we don't have to pick up the new value in the event handler timePicker.SetBinding(TimePicker.ValueProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); timePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(delegate { folder.NotifyPropertyChanged("FirstDue"); folder.NotifyPropertyChanged("FirstDueColor"); }); timePicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(timePicker); break; case DisplayTypes.Checkbox: CheckBox cb = new CheckBox() { DataContext = container, IsTabStop = true }; cb.SetBinding(CheckBox.IsCheckedProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); cb.TabIndex = tabIndex++; EditStackPanel.Children.Add(cb); break; case DisplayTypes.TagList: TextBox taglist = new TextBox() { MinWidth = minWidth, IsTabStop = true }; taglist.KeyUp += new KeyEventHandler(TextBox_KeyUp); taglist.TabIndex = tabIndex++; RenderEditItemTagList(taglist, (Item) container, pi); EditStackPanel.Children.Add(taglist); break; case DisplayTypes.ImageUrl: // TODO: wire up to picture picker, and upload to an image service break; case DisplayTypes.LinkArray: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Url } } }; tb.AcceptsReturn = true; tb.TextWrapping = TextWrapping.Wrap; tb.Height = 150; tb.TabIndex = tabIndex++; tb.ClearValue(TextBox.TextProperty); if (!String.IsNullOrEmpty((string) currentValue)) { try { var linkList = JsonConvert.DeserializeObject<List<Link>>((string)currentValue); tb.Text = String.Concat(linkList.Select(l => l.Name != null ? l.Name + "," + l.Url + "\n" : l.Url + "\n").ToList()); } catch (Exception) { } } tb.LostFocus += new RoutedEventHandler(delegate { // the expected format is a newline-delimited list of Name, Url pairs var linkArray = tb.Text.Split(new char[] { '\r','\n' }, StringSplitOptions.RemoveEmptyEntries); var linkList = new List<Link>(); foreach (var link in linkArray) { var nameval = link.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (nameval.Length == 0) continue; if (nameval.Length == 1) linkList.Add(new Link() { Url = nameval[0].Trim() }); else linkList.Add(new Link() { Name = nameval[0].Trim(), Url = nameval[1].Trim() }); } var json = JsonConvert.SerializeObject(linkList); pi.SetValue(container, json, null); }); EditStackPanel.Children.Add(tb); break; case DisplayTypes.Hidden: // skip rendering break; case DisplayTypes.ContactList: Item currentContacts = CreateValueList(item, field, currentValue == null ? Guid.Empty : new Guid((string) currentValue)); var contactPicker = new ItemRefListPicker(folder, currentContacts, SystemItemTypes.Contact, pi, container) { MinWidth = minWidth, IsTabStop = true }; contactPicker.TabIndex = tabIndex++; contactPicker.MinWidth -= 84; contactPicker.MaxWidth = contactPicker.MinWidth; innerPanel = RenderImageButtonPanel(contactPicker, "/Images/button.add.png", "/Images/button.add.pressed.png"); imageButton = (ImageButton)innerPanel.Children[1]; imageButton.Click += new RoutedEventHandler(delegate { EmailAddressChooserTask chooser = new EmailAddressChooserTask(); chooser.Completed += new EventHandler<EmailResult>((s, e) => { if (e.TaskResult == TaskResult.OK && !String.IsNullOrEmpty(e.Email)) { // find the contact using the email address Contacts contacts = new Contacts(); contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>((sen, ev) => { // save the contact info as a new contact var contact = ev.Results.FirstOrDefault(); if (contact == null) return; var newContact = ContactPickerHelper.ProcessContact(contact); if (newContact != null) { // if the list doesn't yet exist, create it now if (currentContacts.ID == Guid.Empty) { Guid id = Guid.NewGuid(); currentContacts.ID = id; // enqueue the Web Request Record RequestQueue.EnqueueRequestRecord(RequestQueue.UserQueue, new RequestQueue.RequestRecord() { ReqType = RequestQueue.RequestRecord.RequestType.Insert, Body = currentContacts }); // add the list to the folder folder.Items.Add(currentContacts); StorageHelper.WriteFolder(folder); // store the list's Guid in the item's property pi.SetValue(container, id.ToString(), null); } // rebuild the image panel with a new ItemRefListPicker HandleAddedContact(currentContacts, newContact); var oldListPicker = innerPanel.Children[0] as ItemRefListPicker; contactPicker = new ItemRefListPicker(folder, currentContacts, SystemItemTypes.Contact, pi, container) { IsTabStop = true }; if (oldListPicker != null) { contactPicker.TabIndex = oldListPicker.TabIndex; contactPicker.MinWidth = oldListPicker.MinWidth; contactPicker.MaxWidth = oldListPicker.MaxWidth; } innerPanel.Children[0] = contactPicker; } }); contacts.SearchAsync(e.Email, FilterKind.EmailAddress, null); } }); chooser.Show(); }); EditStackPanel.Children.Add(innerPanel); break; case DisplayTypes.LocationList: Item currentLocations = CreateValueList(item, field, currentValue == null ? Guid.Empty : new Guid((string) currentValue)); var locationPicker = new ItemRefListPicker(folder, currentLocations, SystemItemTypes.Location, pi, container) { MinWidth = minWidth, IsTabStop = true }; locationPicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(locationPicker); break; case DisplayTypes.ItemTypes: ListPicker itemTypePicker = new ListPicker() { MinWidth = minWidth, FullModeItemTemplate = (DataTemplate)App.Current.Resources["FullListPickerTemplate"], ExpansionMode = ExpansionMode.FullScreenOnly, IsTabStop = true }; var itemTypes = App.ViewModel.ItemTypes.Where(i => i.UserID != SystemUsers.System).OrderBy(i => i.Name).ToList(); itemTypePicker.ItemsSource = itemTypes; itemTypePicker.DisplayMemberPath = "Name"; ItemType thisItemType = itemTypes.FirstOrDefault(i => i.ID == (Guid) currentValue); itemTypePicker.SelectedIndex = Math.Max(itemTypes.IndexOf(thisItemType), 0); itemTypePicker.SelectionChanged += new SelectionChangedEventHandler(delegate { pi.SetValue(container, itemTypes[itemTypePicker.SelectedIndex].ID, null); }); itemTypePicker.TabIndex = tabIndex++; EditStackPanel.Children.Add(itemTypePicker); break; default: notMatched = true; break; } // if wasn't able to match field type by display type, try matching by FieldType if (notMatched == true) { switch (field.FieldType) { case FieldTypes.String: default: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Text } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, tb.Text, null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); EditStackPanel.Children.Add(tb); break; case FieldTypes.Integer: tb.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = InputScopeNameValue.Digits } } }; tb.LostFocus += new RoutedEventHandler(delegate { pi.SetValue(container, Convert.ToInt32(tb.Text), null); }); tb.TabIndex = tabIndex++; tb.KeyUp += new KeyEventHandler(TextBox_KeyUp); EditStackPanel.Children.Add(tb); break; case FieldTypes.DateTime: DatePicker dp = new DatePicker() { DataContext = container, MinWidth = minWidth, IsTabStop = true }; dp.SetBinding(DatePicker.ValueProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); dp.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(delegate { pi.SetValue(container, dp.Value, null); folder.NotifyPropertyChanged("FirstDue"); folder.NotifyPropertyChanged("FirstDueColor"); }); dp.TabIndex = tabIndex++; EditStackPanel.Children.Add(dp); break; case FieldTypes.Boolean: CheckBox cb = new CheckBox() { DataContext = container, IsTabStop = true }; cb.SetBinding(CheckBox.IsEnabledProperty, new Binding(pi.Name) { Mode = BindingMode.TwoWay }); cb.TabIndex = tabIndex++; EditStackPanel.Children.Add(cb); break; } } // add the listboxitem to the listbox EditListBox.Items.Add(listBoxItem); }
private void clearTextBox(TextBox text) { if (text.Text != String.Empty) text.ClearValue(TextBox.TextProperty); }