I am attempting to write a simple helper class that will make managing Telerik RadGrids in our application consistent.
The relevant portions of the class are shown below:
public delegate object NeedDataSource(object aSender);
public class TelerikGridViewHelper
{
private readonly string _CommandNameDelete; //String checked for in ItemCommand to determine if we have a delete button click
private readonly string _CommandNameEdit; //String checked for in ItemCommand to determine if we have a edit button click
private readonly RadGrid _GridView;
private NeedDataSource _NeedDataSourceCallBack;
private RecordDelete _RecordDeleteCallback;
private RecordEdit _RecordEditCallback;
/// <summary>
/// Class constructor
/// </summary>
/// <param name="aGridView"></param>
public TelerikGridViewHelper(RadGrid aGridView)
{
//Save params supplied
_GridView = aGridView;
//Create resources required
_CommandNameDelete = Guid.NewGuid() + "_Delete";
_CommandNameEdit = Guid.NewGuid() + "_Edit";
Options = new TelerikGridViewHelperOptions();
}
/// <summary>
/// Defines how the grid view will be configured when the Configure() method is called.
/// </summary>
public TelerikGridViewHelperOptions Options { get; set; }
/// <summary>
/// Call this method after setting the appropriate Options to configure the grid view behaviours.
/// </summary>
/// <param name="aKeyFieldNames">List of fields that uniquely identify each record.</param>
/// <param name="aNeedDataSourceCallback">Method returning the contents of the datasource to be displayed by the grid.</param>
/// <param name="aRecordDeleteCallback">Called when a record is to be deleted as a consequence of the user pressing the Delete button automatically added by this class when Options.CommandDelete is true.</param>
/// <param name="aRecordEditCallback">Called when a record is to be edit as a consequence of the user pressing the Edit button automatically added by this class when Options.CommandEdit is true.</param>
public void Configure(string[] aKeyFieldNames, NeedDataSource aNeedDataSourceCallback, RecordDelete aRecordDeleteCallback, RecordEdit aRecordEditCallback)
{
//Save / Apply the parameters supplied
//Datasource
_NeedDataSourceCallBack = aNeedDataSourceCallback;
_RecordDeleteCallback = aRecordDeleteCallback;
_RecordEditCallback = aRecordEditCallback;
//Key fields
_GridView.MasterTableView.DataKeyNames = aKeyFieldNames;
//Now configure the grid based on the configured behaviours
ConfigureGrid(_GridView, Options);
}
/// <summary>
/// Internal helper method to configure the various aspects of the grid based on the options specified.
/// </summary>
/// <param name="aGridView">Gridview to configure.</param>
/// <param name="aOptions">Options to use when configuring the grid.</param>
private void ConfigureGrid(RadGrid aGridView, TelerikGridViewHelperOptions aOptions)
{
//Grid Options
aGridView.AllowFilteringByColumn = aOptions.Filtering;
aGridView.AllowPaging = aOptions.Paging;
aGridView.AutoGenerateColumns = aOptions.AutoGenerateColumns;
aGridView.AutoGenerateHierarchy = true;
aGridView.EnableHeaderContextMenu = true;
aGridView.GroupPanelPosition = GridGroupPanelPosition.Top;
aGridView.Skin = aOptions.ThemeName;
//Table view options
aGridView.MasterTableView.PagerStyle.AlwaysVisible = true;
aGridView.MasterTableView.PagerStyle.Position = GridPagerPosition.TopAndBottom;
aGridView.MasterTableView.PageSize = aOptions.PageSize;
//Events hookup
_GridView.NeedDataSource += CustomGridViewOnNeedDataSource;
}
/// <summary>
/// Hooked to the managed grid view's OnNeedDataSource event. We use this to call what the developer has supplied for
/// the datasource callback.
/// </summary>
/// <param name="aSender">Gridview instance that has fired this event</param>
/// <param name="aEventArgs">Contains the information about the need for the rebind of the datasource.</param>
private void CustomGridViewOnNeedDataSource(object aSender, GridNeedDataSourceEventArgs aEventArgs)
{
(aSender as RadGrid).DataSource = _NeedDataSourceCallBack;
}
/// <summary>
/// Internal helper method to raise the exception class associated with this class
/// </summary>
/// <param name="aExceptionMessage">Message to use when raising the exception.</param>
private void RaiseException(string aExceptionMessage)
{
throw new TelerikGridViewHelperException(aExceptionMessage);
}
}
The class is then invoked from code behind as follows:
public partial class TimeSheetList : BasePage
{
private readonly TimeSheetProcess _TimeSheetProcess;
/// <summary>
/// Class constructor
/// </summary>
public TimeSheetList()
{
_TimeSheetProcess = new TimeSheetProcess();
}
/// <summary>
/// Called when the page is loaded
/// </summary>
/// <param name="aSender"></param>
/// <param name="aEventArgs"></param>
protected void Page_Load(object aSender, EventArgs aEventArgs)
{
TelerikGridViewHelper gridViewHelper = new TelerikGridViewHelper(RadGrid2);
gridViewHelper.Configure(new []{"TimeSheetId"}, DataRetrieve, null, null);
}
private object DataRetrieve(object aSender)
{
//List of timesheets ordered by date and then user within that
DateTime todaysDate = DateTime.Now;
//List of page contents ordered by url
List<Timesheet> timesheetsFromDb = _TimeSheetProcess.GetTimeSheets()
.ToList();
MembershipCache membershipCache = new MembershipCache();
ClockedInBrowseViewModel timesheetsViewModel = new ClockedInBrowseViewModel();
foreach (Timesheet timeSheet in timesheetsFromDb)
{
MembershipUser userInfo = membershipCache.GetUserById(timeSheet.UserId.ToString());
timesheetsViewModel.Items.Add(new ClockedInItemViewModel
{
ClockedInAt = timeSheet.Start.HasValue ? string.Format("{0:dd MMM HH:mm}", timeSheet.Start) : "-",
ClockedOutAt = timeSheet.Finish.HasValue ? string.Format("{0:dd MMM HH:mm}", timeSheet.Finish) : "-",
TimesheetId = timeSheet.TimesheetID,
Username = userInfo.UserName
});
}
return timesheetsViewModel.Items;
}
}
Now the issue that I am having is that the passed in TimeSheetList.DataRetrieve() is not being called and I get the exception "Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource" as soon as the line "(aSender as RadGrid).DataSource = _NeedDataSourceCallBack;" is hit in the helper class. So I am guessing that I have not setup either the hooking on the NeedDataSource event or how the callback is invoked as I know that DataRetrieve() is returning a suitable list of objects.
Can someone point me in the right direction please?
Needed to call Invoke() as follows:
(aSender as RadGrid).DataSource = _NeedDataSourceCallBack.Invoke(aSender);
Related
I've been beating my head against the wall over this for days now and I can't seem to find any info that fits my problem.
So, I have this toolbar Usercontrol that's meant to be dropped in to an application. This toolbar has a property exposed called "FullExtentButton" which is a reference to the button. What I want is to expose the properties of this button in the designer properties pane on the toolbar user control so the developers can set the properties directly from the designer.
In WinForms, this was very easy to do. WPF, not so much (unless I'm just blind).
In my tool bar code:
[Category("Standard Buttons")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public MyToolbarButton FullExtentButton
{
get;
private set;
}
This value is set in the constructor the user control:
public MyToolbar()
{
InitializeComponent();
FullExtentButton = new MyToolbarButton("FullExtent", "/Utilities;component/Resources/full_extent_16x16.png");
}
The button itself is quite simple:
public class MyToolbarButton
: Freezable
{
#region Dependency Properties.
/// <summary>
/// Dependency property for the <see cref="IsVisible"/> property.
/// </summary>
public static DependencyProperty IsVisibleProperty = DependencyProperty.Register("IsVisible", typeof(bool), typeof(MyToolbarButton),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions
.BindsTwoWayByDefault, Visible_Changed));
/// <summary>
/// Dependency property for the <see cref="IsEnabled"/> property.
/// </summary>
public static DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(MyToolbarButton),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Enabled_Changed));
/// <summary>
/// Dependency property for the <see cref="ToolTip"/> property.
/// </summary>
public static DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(string), typeof(MyToolbarButton),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ToolTip_Changed));
/// <summary>
/// Dependency property for the <see cref="Glyph"/> property.
/// </summary>
public static DependencyProperty GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(MyToolbarButton),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Glyph_Changed));
/// <summary>
/// Dependency property for the <see cref="ID"/> property.
/// </summary>
public static DependencyProperty IDProperty = DependencyProperty.Register("ID", typeof(string), typeof(MyToolbarButton),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
/// <summary>
/// Dependency property for the <see cref="ClickedCommand"/> property.
/// </summary>
public static DependencyProperty ClickedCommandProperty = DependencyProperty.Register("ClickedCommand", typeof(IMyRelayCommand<string>),
typeof(MyToolbarButton),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
#endregion
#region Variables.
// The default image source for the button glyph.
private readonly Uri _defaultImageSource;
#endregion
#region Properties.
/// <summary>
/// Property to set or return the command to execute when the button is clicked.
/// </summary>
public IMyRelayCommand<string> ClickedCommand
{
get
{
return (IMyRelayCommand<string>)GetValue(ClickedCommandProperty);
}
set
{
SetValue(ClickedCommandProperty, value);
}
}
/// <summary>
/// Property to set or return the ID of the button.
/// </summary>
public string ID
{
get
{
object value = GetValue(IDProperty);
return value == null ? string.Empty : value.ToString();
}
set
{
SetValue(IDProperty, value);
}
}
/// <summary>
/// Property to set or return the glyph for this button.
/// </summary>
public ImageSource Glyph
{
get
{
return GetValue(GlyphProperty) as ImageSource;
}
set
{
SetValue(GlyphProperty, value);
}
}
/// <summary>
/// Property to set or return the tool tip for the button.
/// </summary>
public string ToolTip
{
get
{
object value = GetValue(ToolTipProperty);
return value == null ? string.Empty : value.ToString();
}
set
{
SetValue(ToolTipProperty, value);
}
}
/// <summary>
/// Property to set or return whether the button is visible or not.
/// </summary>
public bool IsVisible
{
get
{
return (bool)GetValue(IsVisibleProperty);
}
set
{
SetValue(IsVisibleProperty, value);
}
}
/// <summary>
/// Property to set or return whether the button is enabled or not.
/// </summary>
public bool IsEnabled
{
get
{
return (bool)GetValue(IsEnabledProperty);
}
set
{
SetValue(IsEnabledProperty, value);
}
}
#endregion
#region Methods.
/// <summary>
/// Function to handle a change to the <see cref="GlyphProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Glyph_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// Function to handle a change to the <see cref="IsVisibleProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Visible_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// Function to handle a change to the <see cref="IsEnabledProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// When implemented in a derived class, creates a new instance of the <see cref="T:System.Windows.Freezable" /> derived class.
/// </summary>
/// <returns>The new instance.</returns>
protected override Freezable CreateInstanceCore()
{
return new MyToolbarButton();
}
#endregion
#region Constructor/Finalizer.
/// <summary>
/// Initializes a new instance of the <see cref="MyToolbarButton"/> class.
/// </summary>
/// <param name="buttonID">The ID of the button being clicked.</param>
/// <param name="defaultImageSource">The default image source URI for the glyph used by the button.</param>
internal MyToolbarButton(string buttonID, string defaultImageSource)
{
ID = buttonID;
IsVisible = true;
IsEnabled = true;
ToolTip = string.Empty;
if (!string.IsNullOrWhiteSpace(defaultImageSource))
{
_defaultImageSource = new Uri(defaultImageSource, UriKind.Relative);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="MyToolbarButton"/> class.
/// </summary>
public MyToolbarButton()
{
// This is here to keep the XAML designer from complaining.
}
#endregion
But, when I view the button property on my user control in the XAML designer, and expand its properties I get this:
As you can see, there are no properties under that property in the XAML designer. What I want is to have the properties for that button appear under the "FullExtentsButton" property so that my developers can modify the properties, but not be able to create/remove the instance that's already there.
I've tried making the FullExtentButton property on my UserControl a DependencyProperty, but that didn't fix anything.
This is part of a standard toolbar that we want to use across applications, so enforcing consistency is pretty important for us. Plus it will allow our devs to focus on other parts of applications rather than reimplementing the same thing over and over (which is what we're having to do right now).
So, that said, I'm at my wits end here, what am I doing wrong?
Using this code which I had to change somewhat to get it to compile....
/// <summary>
/// Interaction logic for MyToolBarButton.xaml
/// </summary>
public partial class MyToolBarButton : UserControl
{
public MyToolBarButton()
{
InitializeComponent();
}
#region Dependency Properties.
/// <summary>
/// Dependency property for the <see cref="IsVisible"/> property.
/// </summary>
public static DependencyProperty IsVisibleProperty = DependencyProperty.Register("IsVisible", typeof(bool), typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions
.BindsTwoWayByDefault, Visible_Changed));
/// <summary>
/// Dependency property for the <see cref="IsEnabled"/> property.
/// </summary>
public static DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(true,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Enabled_Changed));
/// <summary>
/// Dependency property for the <see cref="ToolTip"/> property.
/// </summary>
public static DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip", typeof(string), typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(ToolTipPropertyChanged)));
private static void ToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
throw new NotImplementedException();
}
/// <summary>
/// Dependency property for the <see cref="Glyph"/> property.
/// </summary>
public static DependencyProperty GlyphProperty = DependencyProperty.Register("Glyph", typeof(ImageSource), typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, Glyph_Changed));
/// <summary>
/// Dependency property for the <see cref="ID"/> property.
/// </summary>
public static DependencyProperty IDProperty = DependencyProperty.Register("ID", typeof(string), typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
/// <summary>
/// Dependency property for the <see cref="ClickedCommand"/> property.
/// </summary>
public static DependencyProperty ClickedCommandProperty = DependencyProperty.Register("ClickedCommand", typeof(string),
typeof(MyOldToolBarButton),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
#endregion
#region Variables.
// The default image source for the button glyph.
public Uri _defaultImageSource { private set; get; }
#endregion
#region Properties.
/// <summary>
/// Property to set or return the ID of the button.
/// </summary>
[Category("Configuration")]
public string ID
{
get
{
object value = GetValue(IDProperty);
return value == null ? string.Empty : value.ToString();
}
set
{
SetValue(IDProperty, value);
}
}
/// <summary>
/// Property to set or return the glyph for this button.
/// </summary>
[Category("Configuration")]
public ImageSource Glyph
{
get
{
return GetValue(GlyphProperty) as ImageSource;
}
set
{
SetValue(GlyphProperty, value);
}
}
/// <summary>
/// Property to set or return the tool tip for the button.
/// </summary>
///
[Category("Configuration")]
public string ToolTip
{
get
{
object value = GetValue(ToolTipProperty);
return value == null ? string.Empty : value.ToString();
}
set
{
SetValue(ToolTipProperty, value);
}
}
/// <summary>
/// Property to set or return whether the button is visible or not.
/// </summary>
///
[Category("Configuration")]
public bool IsVisible
{
get
{
return (bool)GetValue(IsVisibleProperty);
}
set
{
SetValue(IsVisibleProperty, value);
}
}
/// <summary>
/// Property to set or return whether the button is enabled or not.
/// </summary>
///
[Category("Configuration")]
public bool IsEnabled
{
get
{
return (bool)GetValue(IsEnabledProperty);
}
set
{
SetValue(IsEnabledProperty, value);
}
}
#endregion
#region Methods.
/// <summary>
/// Function to handle a change to the <see cref="GlyphProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Glyph_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// Function to handle a change to the <see cref="IsVisibleProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Visible_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// Function to handle a change to the <see cref="IsEnabledProperty"/>.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void Enabled_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
// TODO
}
/// <summary>
/// When implemented in a derived class, creates a new instance of the <see cref="T:System.Windows.Freezable" /> derived class.
/// </summary>
/// <returns>The new instance.</returns>
protected Freezable CreateInstanceCore()
{
return new MyOldToolBarButton();
}
#endregion
#region Constructor/Finalizer.
/// <summary>
/// Initializes a new instance of the <see cref="MyOldToolBarButton"/> class.
/// </summary>
/// <param name="buttonID">The ID of the button being clicked.</param>
/// <param name="defaultImageSource">The default image source URI for the glyph used by the button.</param>
internal void MyOldToolBarButton(string buttonID, string defaultImageSource)
{
ID = buttonID;
IsVisible = true;
IsEnabled = true;
ToolTip = string.Empty;
if (!string.IsNullOrWhiteSpace(defaultImageSource))
{
_defaultImageSource = new Uri(defaultImageSource, UriKind.Relative);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="MyOldToolBarButton"/> class.
/// </summary>
public void MyOldToolBarButton()
{
// This is here to keep the XAML designer from complaining.
}
#endregion
}
And adding it to another "parent" control... The properties look like this:
Is this what you are looking for?
I've set up the binding of a ListView following this example, binding to observable collection but when I run the application the collection values aren't displayed in the ListView.
The output window isn't throwing any binding errors, so not sure what the binding error could be.
Also I've set a breakpoint on the list before its sent to the second VM and it's populated, ie, not null.
My guess is that the list is null in the second VM as it's not being initialized properly after being passed over.
Can anyone advise how to debug the ListView being empty?
This is the binding set in the View:
<ListBox ItemsSource="{Binding AddedSubjectGradePairsCopy}" Height="400" Margin="0,0,0,-329" VerticalAlignment="Top">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Subject}" /><Run Text=" - " /><Run Text="{Binding Points}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The data context for the View is set as follows in the code behind:
namespace LC_Points.View
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ViewSubjectGradePage : Page
{
private NavigationHelper navigationHelper;
private ViewSubjectGradeViewModel ViewModel;
public ViewSubjectGradePage()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
ViewModel = new ViewSubjectGradeViewModel();
this.DataContext = ViewModel;
}
/// <summary>
/// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
/// </summary>
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
/// <summary>
/// Populates the page with content passed during navigation. Any saved state is also
/// provided when recreating a page from a prior session.
/// </summary>
/// <param name="sender">
/// The source of the event; typically <see cref="NavigationHelper"/>
/// </param>
/// <param name="e">Event data that provides both the navigation parameter passed to
/// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
/// a dictionary of state preserved by this page during an earlier
/// session. The state will be null the first time a page is visited.</param>
private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
}
/// <summary>
/// Preserves state associated with this page in case the application is suspended or the
/// page is discarded from the navigation cache. Values must conform to the serialization
/// requirements of <see cref="SuspensionManager.SessionState"/>.
/// </summary>
/// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
/// <param name="e">Event data that provides an empty dictionary to be populated with
/// serializable state.</param>
private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
}
#region NavigationHelper registration
/// <summary>
/// The methods provided in this section are simply used to allow
/// NavigationHelper to respond to the page's navigation methods.
/// <para>
/// Page specific logic should be placed in event handlers for the
/// <see cref="NavigationHelper.LoadState"/>
/// and <see cref="NavigationHelper.SaveState"/>.
/// The navigation parameter is available in the LoadState method
/// in addition to page state preserved during an earlier session.
/// </para>
/// </summary>
/// <param name="e">Provides data for navigation methods and event
/// handlers that cannot cancel the navigation request.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedFrom(e);
}
#endregion
}
}
And receiving the list in the ViewSubjectGradeVM via the constructor:
namespace LC_Points.ViewModel
{
public class ViewSubjectGradeViewModel
{
public ViewSubjectGradeViewModel()
{
}
/// <summary>
/// Initializes a new instance of the ViewSubjectGradeViewModel class.
/// </summary>
public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
{
this.AddedSubjectGradePairsCopy = addedSubjectGradePairs;
}
//Property for collection passed from MainViewModel
public IEnumerable<ScoreModel> AddedSubjectGradePairsCopy { get; set; }
}
}
And this is the backing Model for the List being passed from the MainVM to the ViewSubjectGradeVM:
namespace LC_Points.Model
{
public class ScoreModel : INotifyPropertyChanged
{
// The name of the subject.
public string Subject { get; set; }
// The points paired with each grade type.
public int Points { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Try something like this. Of course, your model should comes form some source which you should map to your viewmodel. But this illustrates you the way to set correct datacontext.
var scoremodels = new List<ScoreModel>
{
new ScoreModel {Subject = "Subj1", Points = 6},
new ScoreModel {Subject = "Subj2", Points = 3},
new ScoreModel {Subject = "Subj3", Points = 8},
}
ViewModel = new ViewSubjectGradeViewModel(scoreModels);
this.DataContext = ViewModel;
I am attempting to build an MVVM Windows Application Using PRISM 5 and I have wrapped my main content window with an AvalonDock (Wrapping Code Below).
using Microsoft.Practices.Prism.Regions;
using Xceed.Wpf.AvalonDock;
using System.Collections.Specialized;
using System.Windows;
using Xceed.Wpf.AvalonDock.Layout;
namespace Central.Adapters
{
using System.Linq;
public class AvalonDockRegionAdapter : RegionAdapterBase<DockingManager>
{
/// <summary>
/// This ties the adapter into the base region factory.
/// </summary>
/// <param name="factory">The factory that determines where the modules will go.</param>
public AvalonDockRegionAdapter(IRegionBehaviorFactory factory)
: base(factory)
{
}
/// <summary>
/// Since PRISM does not support the Avalon DockingManager natively this adapter provides the needed support.
/// </summary>
/// <param name="region">This is the region that resides in the DockingManager.</param>
/// <param name="regionTarget">The DockingManager that needs the window added.</param>
protected override void Adapt(IRegion region, DockingManager regionTarget)
{
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddAnchorableDocument(regionTarget, e);
break;
case NotifyCollectionChangedAction.Remove:
break;
}
};
}
/// <summary>
/// This adds the window as an anchorable document to the Avalon DockingManager.
/// </summary>
/// <param name="regionTarget">The DockingManager instance.</param>
/// <param name="e">The new window to be added.</param>
private static void AddAnchorableDocument(DockingManager regionTarget, NotifyCollectionChangedEventArgs e)
{
foreach (FrameworkElement element in e.NewItems)
{
var view = element as UIElement;
var documentPane = regionTarget.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if ((view == null) || (documentPane == null))
{
continue;
}
var newContentPane = new LayoutAnchorable
{
Content = view,
Title = element.ToolTip.ToString(),
CanHide = true,
CanClose = false
};
documentPane.Children.Add(newContentPane);
}
}
/// <summary>
/// This returns the region instance populated with all of its contents.
/// </summary>
/// <returns>DockingManager formatted region.</returns>
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
}
I then register this adapter in the bootstrapper this way:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var mappings = base.ConfigureRegionAdapterMappings();
if (mappings == null)
{
return null;
}
mappings.RegisterMapping(typeof(DockingManager), new AvalonDockRegionAdapter(ConfigureDefaultRegionBehaviors()));
return mappings;
}
The problem that I am facing is that other region UI elements will require certain LayoutAnchorable windows to become the active and selected window. The content I am feeding into the LayoutAnchorable object is a ContentControl.
In my View's ViewModel I have a property that I am successfully setting using another UI element's interaction. However I am unable to make the connection from ViewModel(Property) -> ContentContro(View) -> LayoutAnchorable(View's Parent).IsSelected or ,IsActive.
I know how to bind to a parent object but that eats up the property and does not allow me to bind it to the ViewModel property as well. I also have no problem binding to a ViewModel property, but that is useless unless I can get it to set the parent property. I have also attempted View based events. Problem with this is that once the view loads it doe not like calling its own events anymore unless it is caused by user interaction directly with that view.
In short I just want to display the appropriate window when needed based on an interaction in another part of my program. Maybe I am making this more complicated than it needs to be. Any assistance on this would be greatly appreciated.
Thanks
James
As I took a break from the problem at had I looked at it from another perspective. To solve the issue I decided to store the instance of the content panes containing the views into a singleton dictionary class:
using System;
using System.Collections.Generic;
using Xceed.Wpf.AvalonDock.Layout;
namespace Central.Services
{
public class DockinWindowChildObjectDictionary
{
private static Dictionary<string, LayoutAnchorable> _contentPane = new Dictionary<string, LayoutAnchorable>();
private static readonly Lazy<DockinWindowChildObjectDictionary> _instance =
new Lazy<DockinWindowChildObjectDictionary>(()=> new DockinWindowChildObjectDictionary(), true);
public static DockinWindowChildObjectDictionary Instance
{
get
{
return _instance.Value;
}
}
/// <summary>
/// Causes the constructor to be private allowing for proper use of the Singleton pattern.
/// </summary>
private DockinWindowChildObjectDictionary()
{
}
/// <summary>
/// Adds a Content Pane instance to the dictionary.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
/// <param name="contentPane">The object instance.</param>
public static void Add(string title, LayoutAnchorable contentPane)
{
_contentPane.Add(title, contentPane);
}
/// <summary>
/// If a window needs to be removed from the dock this should be used
/// to also remove it from the dictionary.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
public static void Remove(string title)
{
_contentPane.Remove(title);
}
/// <summary>
/// This will return the instance of the content pane that holds the view.
/// </summary>
/// <param name="title">The title given to the Pane during instantiation.</param>
/// <returns>The views Parent Instance.</returns>
public static LayoutAnchorable GetInstance(string title)
{
return _contentPane[title];
}
}
}
In the adapter I modified this code as follows:
private static void AddAnchorableDocument(DockingManager regionTarget, NotifyCollectionChangedEventArgs e)
{
foreach (FrameworkElement element in e.NewItems)
{
var view = element as UIElement;
var documentPane = regionTarget.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
if ((view == null) || (documentPane == null))
{
continue;
}
var newContentPane = new LayoutAnchorable
{
Content = view,
Title = element.ToolTip.ToString(),
CanHide = true,
CanClose = false
};
DockinWindowChildObjectDictionary.Add(element.ToolTip.ToString(),** newContentPane);
documentPane.Children.Add(newContentPane);
}
}
Then I added the following to the ViewModel to gain the effect I was going after:
public void OnNavigatedTo(NavigationContext navigationContext)
{
var viewParentInstance = DockinWindowChildObjectDictionary.GetInstance("Belt Plan");
viewParentInstance.IsSelected = true;
}
One hurdle done and on to the next. For a base to all the information in this post the ViewSwitchingNavigation.sln included with the PRISM 5.0 download will get you started. If you are wondering about the ConfigureDefaultRegionBehaviors() referenced in the adapter registration I got that from the StockTraderRI_Desktop.sln in the sample downloads.
I hope this post helps someone else that finds themselves in the same pickle this technology sandwich provides.
Sincerely
James
Basic Requirement - Displaying In App tiles in Universal Apps.
I have gone through samples of Hub Tiles from the following link Hub Tiles which applies to Windows Phone 7 and 8, 8.1 (Silverlight), but there is no mentioning of windows Phone 8.1 (Universal Applications). Even if i include the assembly separately it throws an error that Microsoft.Phone cannot be resolved.
As a work around I have even downloaded the source of hub Tiles from Windows phone Toolkit Source and resolved the assemblies, modified the templates and edited it accordingly. But I might be missing on some points, Although It is showing as a control in the tool box but it is not even visible when I add it to XAML. I am adding the edited code for all the three files available from the source of the toolkit.
HubTiles.cs
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System.Windows;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
namespace Microsoft.Phone.Controls
{
/// <summary>
/// Represents an animated tile that supports an image and a title.
/// Furthermore, it can also be associated with a message or a notification.
/// </summary>
/// <QualityBand>Preview</QualityBand>
[TemplateVisualState(Name = Expanded, GroupName = ImageStates)]
[TemplateVisualState(Name = Semiexpanded, GroupName = ImageStates)]
[TemplateVisualState(Name = Collapsed, GroupName = ImageStates)]
[TemplateVisualState(Name = Flipped, GroupName = ImageStates)]
[TemplatePart(Name = NotificationBlock, Type = typeof(TextBlock))]
[TemplatePart(Name = MessageBlock, Type = typeof(TextBlock))]
[TemplatePart(Name = BackTitleBlock, Type = typeof(TextBlock))]
[TemplatePart(Name = TitlePanel, Type = typeof(Panel))]
public class HubTile : Control
{
/// <summary>
/// Common visual states.
/// </summary>
private const string ImageStates = "ImageState";
/// <summary>
/// Expanded visual state.
/// </summary>
private const string Expanded = "Expanded";
/// <summary>
/// Semiexpanded visual state.
/// </summary>
private const string Semiexpanded = "Semiexpanded";
/// <summary>
/// Collapsed visual state.
/// </summary>
private const string Collapsed = "Collapsed";
/// <summary>
/// Flipped visual state.
/// </summary>
private const string Flipped = "Flipped";
/// <summary>
/// Nofitication Block template part name.
/// </summary>
private const string NotificationBlock = "NotificationBlock";
/// <summary>
/// Message Block template part name.
/// </summary>
private const string MessageBlock = "MessageBlock";
/// <summary>
/// Back Title Block template part name.
/// </summary>
private const string BackTitleBlock = "BackTitleBlock";
/// <summary>
/// Title Panel template part name.
/// </summary>
private const string TitlePanel = "TitlePanel";
/// <summary>
/// Notification Block template part.
/// </summary>
private TextBlock _notificationBlock;
/// <summary>
/// Message Block template part.
/// </summary>
private TextBlock _messageBlock;
/// <summary>
/// Title Panel template part.
/// </summary>
private Panel _titlePanel;
/// <summary>
/// Back Title Block template part.
/// </summary>
private TextBlock _backTitleBlock;
/// <summary>
/// Represents the number of steps inside the pipeline of stalled images
/// </summary>
internal int _stallingCounter;
/// <summary>
/// Flag that determines if the hub tile has a primary text string associated to it.
/// If it does not, the hub tile will not drop.
/// </summary>
internal bool _canDrop;
/// <summary>
/// Flag that determines if the hub tile has a secondary text string associated to it.
/// If it does not, the hub tile will not flip.
/// </summary>
internal bool _canFlip;
#region Source DependencyProperty
/// <summary>
/// Gets or sets the image source.
/// </summary>
public ImageSource Source
{
get { return (ImageSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
/// <summary>
/// Identifies the Source dependency property.
/// </summary>
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(ImageSource), typeof(HubTile), new PropertyMetadata(null));
#endregion
#region Title DependencyProperty
/// <summary>
/// Gets or sets the title.
/// </summary>
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
/// <summary>
/// Identifies the Title dependency property.
/// </summary>
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnTitleChanged)));
/// <summary>
/// Prevents the hub tile from transitioning into a Semiexpanded or Collapsed visual state if the title is not set.
/// </summary>
/// <param name="obj">The dependency object.</param>
/// <param name="e">The event information.</param>
private static void OnTitleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
HubTile tile = (HubTile)obj;
if (string.IsNullOrEmpty((string)e.NewValue))
{
tile._canDrop = false;
tile.State = ImageState.Expanded;
}
else
{
tile._canDrop = true;
}
}
#endregion
#region Notification DependencyProperty
/// <summary>
/// Gets or sets the notification alert.
/// </summary>
public string Notification
{
get { return (string)GetValue(NotificationProperty); }
set { SetValue(NotificationProperty, value); }
}
/// <summary>
/// Identifies the Notification dependency property.
/// </summary>
public static readonly DependencyProperty NotificationProperty =
DependencyProperty.Register("Notification", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnBackContentChanged)));
/// <summary>
/// Prevents the hub tile from transitioning into a Flipped visual state if neither the notification alert nor the message are set.
/// </summary>
/// <param name="obj">The dependency object.</param>
/// <param name="e">The event information.</param>
private static void OnBackContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
HubTile tile = (HubTile)obj;
// If there is a new notification or a message, the hub tile can flip.
if ((!(string.IsNullOrEmpty(tile.Notification)) && tile.DisplayNotification)
|| (!(string.IsNullOrEmpty(tile.Message)) && !tile.DisplayNotification))
{
tile._canFlip = true;
}
else
{
tile._canFlip = false;
tile.State = ImageState.Expanded;
}
}
#endregion
#region Message DependencyProperty
/// <summary>
/// Gets or sets the message.
/// </summary>
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
/// <summary>
/// Identifies the Message dependency property.
/// </summary>
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnBackContentChanged)));
#endregion
#region DisplayNotification DependencyProperty
/// <summary>
/// Gets or sets the flag for new notifications.
/// </summary>
public bool DisplayNotification
{
get { return (bool)GetValue(DisplayNotificationProperty); }
set { SetValue(DisplayNotificationProperty, value); }
}
/// <summary>
/// Identifies the DisplayNotification dependency property.
/// </summary>
public static readonly DependencyProperty DisplayNotificationProperty =
DependencyProperty.Register("DisplayNotification", typeof(bool), typeof(HubTile), new PropertyMetadata(false, new PropertyChangedCallback(OnBackContentChanged)));
#endregion
#region IsFrozen DependencyProperty
/// <summary>
/// Gets or sets the flag for images that do not animate.
/// </summary>
public bool IsFrozen
{
get { return (bool)GetValue(IsFrozenProperty); }
set { SetValue(IsFrozenProperty, value); }
}
/// <summary>
/// Identifies the IsFrozen dependency property.
/// </summary>
public static readonly DependencyProperty IsFrozenProperty =
DependencyProperty.Register("IsFrozen", typeof(bool), typeof(HubTile), new PropertyMetadata(false, new PropertyChangedCallback(OnIsFrozenChanged)));
/// <summary>
/// Removes the frozen image from the enabled image pool or the stalled image pipeline.
/// Adds the non-frozen image to the enabled image pool.
/// </summary>
/// <param name="obj">The dependency object.</param>
/// <param name="e">The event information.</param>
private static void OnIsFrozenChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
HubTile tile = (HubTile)obj;
if ((bool)e.NewValue)
{
HubTileService.FreezeHubTile(tile);
}
else
{
HubTileService.UnfreezeHubTile(tile);
}
}
#endregion
#region GroupTag DependencyProperty
/// <summary>
/// Gets or sets the group tag.
/// </summary>
public string GroupTag
{
get { return (string)GetValue(GroupTagProperty); }
set { SetValue(GroupTagProperty, value); }
}
/// <summary>
/// Identifies the GroupTag dependency property.
/// </summary>
public static readonly DependencyProperty GroupTagProperty =
DependencyProperty.Register("GroupTag", typeof(string), typeof(HubTile), new PropertyMetadata(string.Empty));
#endregion
#region State DependencyProperty
/// <summary>
/// Gets or sets the visual state.
/// </summary>
internal ImageState State
{
get { return (ImageState)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}
/// <summary>
/// Identifies the State dependency property.
/// </summary>
private static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(ImageState), typeof(HubTile), new PropertyMetadata(ImageState.Expanded, OnImageStateChanged));
/// <summary>
/// Triggers the transition between visual states.
/// </summary>
/// <param name="obj">The dependency object.</param>
/// <param name="e">The event information.</param>
private static void OnImageStateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
((HubTile)obj).UpdateVisualState();
}
#endregion
#region Size DependencyProperty
/// <summary>
/// Gets or sets the visual state.
/// </summary>
public TileSize Size
{
get { return (TileSize)GetValue(SizeProperty); }
set { SetValue(SizeProperty, value); }
}
/// <summary>
/// Identifies the State dependency property.
/// </summary>
public static readonly DependencyProperty SizeProperty =
DependencyProperty.Register("Size", typeof(TileSize), typeof(HubTile), new PropertyMetadata(TileSize.Default, OnSizeChanged));
/// <summary>
/// Triggers the transition between visual states.
/// </summary>
/// <param name="obj">The dependency object.</param>
/// <param name="e">The event information.</param>
private static void OnSizeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
HubTile hubTile = (HubTile)obj;
// And now we'll update the width and height to match the new size.
switch (hubTile.Size)
{
case TileSize.Default:
hubTile.Width = 173;
hubTile.Height = 173;
break;
case TileSize.Small:
hubTile.Width = 99;
hubTile.Height = 99;
break;
case TileSize.Medium:
hubTile.Width = 210;
hubTile.Height = 210;
break;
case TileSize.Large:
hubTile.Width = 432;
hubTile.Height = 210;
break;
}
hubTile.SizeChanged += OnHubTileSizeChanged;
HubTileService.FinalizeReference(hubTile);
}
static void OnHubTileSizeChanged(object sender, SizeChangedEventArgs e)
{
HubTile hubTile = (HubTile)sender;
hubTile.SizeChanged -= OnHubTileSizeChanged;
// In order to avoid getting into a bad state, we'll shift the HubTile
// back to the Expanded state. If we were already in the Expanded state,
// then we'll manually shift the title panel to the right location,
// since the visual state manager won't do it for us in that case.
if (hubTile.State != ImageState.Expanded)
{
hubTile.State = ImageState.Expanded;
VisualStateManager.GoToState(hubTile, Expanded, false);
}
else if (hubTile._titlePanel != null)
{
CompositeTransform titlePanelTransform = hubTile._titlePanel.RenderTransform as CompositeTransform;
if (titlePanelTransform != null)
{
titlePanelTransform.TranslateY = -hubTile.Height;
}
}
HubTileService.InitializeReference(hubTile);
}
#endregion
/// <summary>
/// Updates the visual state.
/// </summary>
private void UpdateVisualState()
{
string state;
// If we're in the Small size, then we should just display the image
// instead of having animations.
if (Size != TileSize.Small)
{
switch (State)
{
case ImageState.Expanded:
state = Expanded;
break;
case ImageState.Semiexpanded:
state = Semiexpanded;
break;
case ImageState.Collapsed:
state = Collapsed;
break;
case ImageState.Flipped:
state = Flipped;
break;
default:
state = Expanded;
break;
}
}
else
{
state = Expanded;
}
VisualStateManager.GoToState(this, state, true);
}
/// <summary>
/// Gets the template parts and sets binding.
/// </summary>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_notificationBlock = base.GetTemplateChild(NotificationBlock) as TextBlock;
_messageBlock = base.GetTemplateChild(MessageBlock) as TextBlock;
_backTitleBlock = base.GetTemplateChild(BackTitleBlock) as TextBlock;
_titlePanel = base.GetTemplateChild(TitlePanel) as Panel;
//Do binding in code to avoid exposing unnecessary value converters.
if (_notificationBlock != null)
{
Binding bindVisible = new Binding();
bindVisible.Source = this;
bindVisible.Path = new PropertyPath("DisplayNotification");
bindVisible.Converter = new VisibilityConverter();
bindVisible.ConverterParameter = false;
_notificationBlock.SetBinding(TextBlock.VisibilityProperty, bindVisible);
}
if(_messageBlock != null)
{
Binding bindCollapsed = new Binding();
bindCollapsed.Source = this;
bindCollapsed.Path = new PropertyPath("DisplayNotification");
bindCollapsed.Converter = new VisibilityConverter();
bindCollapsed.ConverterParameter = true;
_messageBlock.SetBinding(TextBlock.VisibilityProperty, bindCollapsed);
}
if(_backTitleBlock != null)
{
Binding bindTitle = new Binding();
bindTitle.Source = this;
bindTitle.Path = new PropertyPath("Title");
bindTitle.Converter = new MultipleToSingleLineStringConverter();
_backTitleBlock.SetBinding(TextBlock.TextProperty, bindTitle);
}
UpdateVisualState();
}
/// <summary>
/// Initializes a new instance of the HubTile class.
/// </summary>
public HubTile()
{
DefaultStyleKey = typeof(HubTile);
Loaded += HubTile_Loaded;
Unloaded += HubTile_Unloaded;
}
/// <summary>
/// This event handler gets called as soon as a hub tile is added to the visual tree.
/// A reference of this hub tile is passed on to the service singleton.
/// </summary>
/// <param name="sender">The hub tile.</param>
/// <param name="e">The event information.</param>
void HubTile_Loaded(object sender, RoutedEventArgs e)
{
HubTileService.InitializeReference(this);
}
/// <summary>
/// This event handler gets called as soon as a hub tile is removed from the visual tree.
/// Any existing reference of this hub tile is eliminated from the service singleton.
/// </summary>
/// <param name="sender">The hub tile.</param>
/// <param name="e">The event information.</param>
void HubTile_Unloaded(object sender, RoutedEventArgs e)
{
HubTileService.FinalizeReference(this);
}
}
/// <summary>
/// Represents the visual states of a Hub tile.
/// </summary>
internal enum ImageState
{
/// <summary>
/// Expanded visual state value.
/// </summary>
Expanded = 0,
/// <summary>
/// Semiexpanded visual state value.
/// </summary>
Semiexpanded = 1,
/// <summary>
/// Collapsed visual state value.
/// </summary>
Collapsed = 2,
/// <summary>
/// Flipped visual state value.
/// </summary>
Flipped = 3,
};
/// <summary>
/// Represents the size of a Hub tile.
/// </summary>
public enum TileSize
{
/// <summary>
/// Default size (173 px x 173 px).
/// </summary>
Default,
/// <summary>
/// Small size (99 px x 99 px).
/// </summary>
Small,
/// <summary>
/// Medium size (210 px x 210 px).
/// </summary>
Medium,
/// <summary>
/// Large size (432 px x 210 px).
/// </summary>
Large,
};
}
Note: We can edit the same for HubTileConverters and HubTileService but still it doesnt work.
If any one has a solution or has edited the same for Universal Application Or if there is any Toolkit from Microsoft for the same then please let me know.
TIA.
I have ported the toolkit HubTile stuff. Done with a sample for your refrence please find in the following link.
HubTile sample for universal app
Hope this will help you.
Maybe you should check the Generic.xaml file in toolkit source code, it is placed in Themes folder. All toolkit control default styles are stored in this file. It seems your problem is lack of this style file.
Because HubTile is a custom control, it will read the \Themes\Generic.xaml in solution by using DefaultStyleKey = typeof(HubTile) in its constructor. You should port this file to your solution.
I need to change the text of the Cancel button to always display "Reset" in my app.
I found many similar questions here on SO, but all answers are for ObjC, while I am working in Monotouch.
Use the extension method below for iOS7+ like this: searchBar.SetCancelTitle("close");
It derives from Kolbjørn Bredrup's comment, which is great, but it wasn't clear to me what the namespaces were for the additional extension methods that it uses internally (OfType and FirstOrDefault) so I added them to this class myself.
I also renamed the class UISearchBarExtensions so that it sat nicely with my other ones.
Thanks to Kolbjørn Bredrup for the original!
As with the original, it is called using:
searchBar.SetCancelTitle("close");
public static class UISearchBarExtensions
{
/// <summary>
/// Finds the Cancelbutton
/// </summary>
/// <returns></returns>
public static UIButton GetCancelButton(this UISearchBar searchBar)
{
//Look for a button, probably only one button, and that is probably the cancel button.
return (UIButton)searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault();
}
public static UIView FirstOrDefault(this IEnumerable<UIView> views)
{
return ((List<UIView>)views)[0];
}
public static IEnumerable<UIView> OfType<T>(this IEnumerable<UIView> views)
{
List<UIView> response = new List<UIView>();
foreach(var view in views)
{
if(view.GetType() == typeof(T))
{
response.Add(view);
}
}
return response;
}
/// <summary>
/// Recursively traverses all subviews and returns them in a little list.
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
public static IEnumerable<UIView> GetAllSubViews(this UIView view)
{
List<UIView> retList = new List<UIView>();
retList.AddRange(view.Subviews);
foreach (var subview in view.Subviews)
{
retList.AddRange(subview.GetAllSubViews());
}
return retList;
}
/// <summary>
/// Sets the title of the search bars cancel button
/// </summary>
/// <param name="searchBar"></param>
/// <param name="cancelTitle"></param>
public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle)
{
searchBar.GetCancelButton().SetTitle(cancelTitle, UIControlState.Normal);
}
}
Note that the subview hierarchy is different in ios6 and ios7.
But by adding the extension method below you can use
searchBar.SetCancelTitle("NO!");
Extension method that works in ios7 and ios5/ios6:
public static class NimbleSearchBarExtensions
{
/// <summary>
/// Finds the Cancelbutton
/// </summary>
/// <returns></returns>
public static UIButton GetCancelButton(this UISearchBar searchBar)
{
//Look for a button, probably only one button, and that is probably the cancel button.
return searchBar.GetAllSubViews().OfType<UIButton>().FirstOrDefault();
}
/// <summary>
/// Recursively traverses all subviews and returns them in a little list.
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
public static IEnumerable<UIView> GetAllSubViews(this UIView view)
{
List<UIView> retList = new List<UIView>();
retList.AddRange(view.Subviews);
foreach (var subview in view.Subviews)
{
retList.AddRange(subview.GetAllSubViews());
}
return retList;
}
/// <summary>
/// Sets the title of the search bars cancel button
/// </summary>
/// <param name="searchBar"></param>
/// <param name="cancelTitle"></param>
public static void SetCancelTitle(this UISearchBar searchBar, string cancelTitle)
{
searchBar.GetCancelButton().SetTitle(cancelTitle,UIControlState.Normal);
}
}
It was very easy -
UIButton btnCancel = (UIButton)SearchBar.Subviews[3];
btnCancel.SetTitle ("test", UIControlState.Normal);