Check checkbox of selected rows within DataGrid - silverlight-5.0

I have one datagrid, within which first column contains checkbox. What I want to implement is, when I check first row checkbox and then i check fifth row checkbox keeping shift button pressed then application should check all the checkbox of the selected rows (i.e. row first to fifth).
I am using MVVM pattern. I have checkall checkbox at the top of the checkbox column and command is binded to it. CheckAll checkbox working properly. Click on individual checkbox also executes command from the viewmodel which checks-unchecks selected checkbox. ViewModel's property is binded to checkbox.
What I want to do now is very similar what we see in mails (i.e. yahoo,gmail). Checkbox should check-uncheck base on row selection from keyboard using shift-key. Please inform me if I am missing out any required information for describing my issue.

Yup possible.
In your Datagrid that you used there is a property called SELECETIONMODE.
Set this to SelectionMode = EXTENDED,
Next->
Use this kind of code to check all the Checkboxes to true.
It is best to create a class of the data your loading. Makes it clean and easy.
If your using RIA services to load data and bind it to your datagrid your already sorted..:)
Instead of using column numbers i would recommend this highly.
private void refreshDatagrid()
{
ObservableCollection<Cars> c = --Load all your cars----;
this.mydatagrid.ItemsSource = c;
}
---To Check all the boxes
private void checkAllBoxes_Click(object sender, RoutedEventArgs e)
{
if (this.mydatagrid.SelectedItems != null)
{
foreach (Cars c in this.mydatagrid.SelectedItems)
{
if (c.isSelected != true)
{
ula.Approved = true;
}
}
}
}
--Example Object Class
class Cars {
public Cars(){}
public Nullable<bool> isSelected {get;set;}
public string carName {get;set;}
public string LicenseNumber {get;set;}
public string year {get;set;}
public string model {get;set;}
}
Of course finally Create a button in the frontend with Click="checkAllBoxes_Click"
Hope the concept is clear.
Cheers

Related

MAUI Bind to model to show last element in a list

I am a bit stumped, please help.
I have a model like this:
public class ExpenseReport
{
public List<Comment> Comments { get; set; }
public string Currency { get; set; }
etc
}
Now in my View I need to show the most recent comment for this ExpenseReport, and also show a "+" button which would show all the comments for this ExpenseReport in a new view.
How do I bind to the latest comment ?
What I have tried:
Have a hidden label like this and somehow read it in another Label control further down my xaml page:
but I don't think this could ever work.
In the code-behind of the view have some code in OnAppearing() which would read the ID of the ExpenseReport, query again the datasource for this specific ID, get the list of comments, and get the latest one and assign it to the text property of a named label in the view.
I have no idea on how to do this in the viewmodel at all.
Thank you
Alex
create a read only property
public Comment MostRecentComment
{
get {
return Comments?.LastOrDefault();
}
}

Sitecore custom ribbon button not working

I have created a custom ribbon button following the steps mentioned in http://jondjones.com/how-to-add-a-custom-sitecore-button-to-the-editor-ribbon/
I can see the button appearing in sitecore:
Custom button
Command does not get triggered when clicked on the button.
Below is my code:
using System;
using Sitecore.Shell.Applications.Dialogs.ProgressBoxes;
using Sitecore.Shell.Framework.Commands;
namespace SitecoreVsPoc.Commands
{
public class TranslateContent : Command
{
private static readonly object Monitor = new object();
public override void Execute(CommandContext context)
{
if (context == null)
return;
try
{
ProgressBox.Execute("Arjun", "Title", "Applications/32x32/refresh.png", Refresh);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error!", ex, this);
}
}
public void Refresh(params object[] parameters)
{
// Do Stuff
}
}
}
Below is the command I have registered in commands.config:
<command name="contenteditor:translatecontent" type="SitecoreVsPoc.Commands.TranslateContent,SitecoreVsPoc" />
Note: I am using Sitecore 8.2 initial release.
Can someone suggest a solution for this?
In Sitecore 8 it was changed the way you add Ribbon button. As far I see your link is from Sitecore 7 or 6.
To create the new button item for the Experience Editor ribbon:
In the Core database, open the Content Editor and navigate to /sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Page Editor/Edit.
Create a new item based on the relevant ribbon control template, for example, the Small Button template. The templates are located at /sitecore/templates/System/Ribbon/.
For the new item, add the following information:
In the Header field, enter the display name of the button.
In the ID field, enter a unique identifier for the item. For example, you can include the ribbon group name in the ID.
In the Icon field, enter the path to the relevant icon. Depending on the button you create, adjust the icon size accordingly.
Open Sitecore Rocks and add the relevant control rendering, for example SmallButton, to the layout of the button item you created.
Enter a unique ID for the rendering.
For other SPEAK controls, you can point to another item in the Data Source field and specify the configuration in this other item.
Important
More informations you can find here: https://doc.sitecore.net/sitecore_experience_platform/content_authoring/the_editing_tools/the_experience_editor/customize_the_experience_editor_ribbon
http://reyrahadian.com/2015/04/15/sitecore-8-adding-edit-meta-data-button-in-experience-editor/
Before it was very simple, you didn't need to add new code:
https://blog.istern.dk/2012/05/21/running-sitecore-field-editor-from-a-command/

Sitecore get all parent (ancestor) with little processing (performance)

I'm trying to come up with solution on getting all the ancestors for the context item. One option is to store _path in index and other option is to do similar to one below:
http://www.glass.lu/Blog/GettingAncestors
I'm having no luck with getting the solution work for above (glass mapper) solution.
I got the index solution working but would like to avoid using index just to get the _path (collection of ancestors) as we don't have any other requirements to use index e.g. search etc.
Appreciate if someone could share the snippet for working solution or even better if Glassmapper has already included the above blog solution.
The most efficient way to check if one item is a descendant of another is to simply check that the current item property Paths.LongID starts with the LongID of the parent item:
Item currentItem = Sitecore.Context.Item;
IList<Item> menuItems = GetMenuItems();
foreach (var menuItem in menuItems)
{
bool isActive = currentItem.Paths.LongID.StartsWith(menuItem.Paths.LongID);
// do code
}
This will work since the path GUIDs are unique for each item.
Alternatively, if you want to use Glass models only then you can use the SitecoreInfoType.FullPath attribute:
[SitecoreInfo(SitecoreInfoType.FullPath)]
public virtual string FullPath { get; private set; }
And then from your code you can simply check:
Item currentItem = Sitecore.Context.Item; //or use SitecoreContext() to get a strongly types model
IEnumerable<MenuItem> menuItems = GetMenuItems();
foreach (var menuItem in menuItems)
{
bool isActive = currentItem.Paths.FullPath.StartsWith(menuItem.FullPath);
// do code
}
Just a word of warning, since each menu item now need code to run in order to determine state, this will make your menu component difficult to cache, resulting caching too many variations or caching per page. You would be better to move this logic into Javascript to set the menu state using the current page URL, this will allow your component to be cached once for all pages.

Issue trying to use Datasource Template in Sitecore sublayout; getting empty Sublayout.Datasource

We've been trying to "componentize" our Sitecore solution as we move forward, in prep for transitioning to Page Editor usage (Woot! Finally!), but we're still practically working primarily with Page templates that may be inheritance-based composites of page specific fields, plus 1:many of these componentized templates. An example of how this looks in our solution is below -- Banner Feature Carousel and Featured Cartoon are some of these new components we're creating:
In the interest of trying to move away from using Sitecore.Context.Item (as I was recently reminded by this post) I've started filling in the Datasource template field on the sublayouts for the new components, and it seems like I've got the appropriate connections made between presentation details, the Sitecore sublayout and the .NET code file (as far as I can tell; again, we're newer to working this way).
I've also tried setting up a base class for these components as per this post by Nick Allen, but here's where I'm running into a problem: When I execute my code, this base class is finding the component Sublayout appropriately (the whole "this.Parent as Sublayout" thing) but, when I go to interrogate the Sublayout.Datasource property, it's an empty string. Here's my code (so far) for this base class:
public class ComponentBase : System.Web.UI.UserControl
{
private Sublayout Sublayout { get { return Parent as Sublayout; } }
public Item DataSourceItem
{
get
{
return Sublayout != null && !String.IsNullOrEmpty(Sublayout.DataSource) ?
Sitecore.Context.Database.GetItem(Sublayout.DataSource) : Sitecore.Context.Item;
}
}
}
I'm apparently missing some interplay between the Datasource Template field in the Sitecore sublayout, and how that actually translates to a datasource. Is it because these component templates are being used to compose Page templates? I was thinking that the datasource would just ultimately resolve to the Page template on which the component in question was currently being used, but perhaps that's my misunderstanding.
If anyone could give me any hints of things to check or point me to any resources I might use to get further, I'd appreciate it. I've done quite a bit of asking the Googs, myself, but am just not getting anything that's helping.
Thank you in advance, Sitecore friends!
It looks like you have configured the allowed templates for your sublayout in the steps you describe above. This basically tells Sitecore; 'allow user to select items based on these templates for this sublayout'. This alone does not set up the data source on items using the sublayout. You still need to go into the presentation details for any items using this sublayout, select the sublayout and then set its datasource property (within the content editor go to Presentation > Details > [Sublayout] > Data Source).
My answer to this question gives the source code needed to retrieve the datasource item and to iterate through the sitecore controls on your sublayout setting all of their Item propertys.
Here is the code:
public class SublayoutBase : UserControl
{
private Item _dataSource;
public Item DataSource
{
get
{
if (_dataSource == null)
{
if (Parent is Sublayout)
{
_dataSource =
Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
}
if (_dataSource == null)
{
_dataSource = Sitecore.Context.Item;
}
}
return _dataSource;
}
}
protected override void OnLoad(EventArgs e)
{
foreach (Control c in Controls)
{
SetFieldRenderers(DataSource, c);
}
base.OnLoad(e);
}
private void SetFieldRenderers(Item item, Control control)
{
if (item != null)
{
var ctrl = control as Sitecore.Web.UI.WebControl;
if (ctrl != null && !string.IsNullOrEmpty(ctrl.DataSource))
{
//don't set the source item if the DataSource has already been set.
return;
}
if (control is FieldRenderer)
{
var fr = (FieldRenderer)control;
fr.Item = item;
}
else if (control is Image)
{
var img = (Image)control;
img.Item = item;
}
else if (control is Link)
{
var link = (Link)control;
link.Item = item;
}
else if (control is Text)
{
var text = (Text)control;
text.Item = item;
}
else
{
foreach (Control childControl in control.Controls)
{
SetFieldRenderers(item, childControl);
}
}
}
}
}
When you start using sublayouts on which you will set your datasource try to use the marketplace "Sublayout Parameter Helper". When you use the class they provide as base class and have set everything up right in your Sitecore Environment you will find yourself having a "this.DatasourceItem" -> which will contain your datasource Item, or the context Item if none is given.
http://marketplace.sitecore.net/en/Modules/Sub_Layout_Parameter_Helper.aspx
To find out more on how datasource works, try searching on http://sdn.sitecore.net. There is alot of documentation available that should get you in the right direction. Generally it will be enough to just add a datasource in the sublayout that you can access in your sublayout.
When you read a datasource from a sublayout all it will do is check if the datasource field on that sublayout is filled and will return you information regarding this datasource. So when you add a new component to a page using the page editor (for example a sublayout for a news article) and you have a datasource template defined and filled in during adding this components, you will see that when you check the presentation details on that page a sublyaout with a datasource has been added. The datasource will point to a folder in which you will find an item based on the datasource template you specified on your sublayout. You should also double check this, cause if no datasource is present on the added sublayout it will be obvious you can't access it from your code.
If you are using Sitecore 7 update 1 you can actually now use the attributes. From the Release Notes:
To make it easier to get the data source for a sublayout from
code-behind, the data source is now transferred to the sublayout
control in a "sc_datasource" attribute. (320768)
To get the data source from code, simply refer to
this.Attributes["sc_datasource"];

In Sitecore how to open a custom Sitecore app by right clicking on a content item?

I want to be able to right click on a content item in Sitecore and then select something like "Run My App" in the context menu. Then in the app that runs I need to be able to reference the content item that was right clicked. Is this possible?
Yes you can do this, its not as hard as it sounds.
You want to drop into the Core database and open up the content editor. The right click menu is defined within the sitecore/content/Applications/Content Editor/Context Menus/Default
The items within that folder are what you see when you right-click an item in the tree. So you can add a new item there with a template of Menu Item.
If you look at the existing ones, most of them send a message to the Sitecore Desktop. These messages are the commands defined in /App_Config/Commands.config. I can't see anything in there that would just launch another Sitecore application, so you would need to create a new command to do that. To create one, just inherit from the Sitecore.Shell.Framework.Commands.Command class. That passes in a CommandContext which will holds a collection of Items.
public class DemoCommand: Command
{
#region Overrides of Command
/// <summary>
/// Executes the command in the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
var parameters = new NameValueCollection();
if (context.Items != null && context.Items.Length == 1)
{
var item = context.Items[0];
parameters["id"] = item.ID.ToString();
}
Context.ClientPage.Start(this, "Run", parameters);
}
#endregion
public CommandState QueryStat(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
return CommandState.Enabled;
}
protected static void Run(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
SheerResponse.CheckModified(false);
SheerResponse.Broadcast(
SheerResponse.ShowModalDialog(
"[Path to your application here]"
),
"Shell");
}
}
To get the item passed over, in your message call - just pass the variable $Target.
So the field Message in the Menu Item would be something like:
item:runMyApplication(id=$Target)
Hope that makes sense :)