I'm getting a Image from sitecore with glass
[SitecoreField(FieldName = "Icon Image")]
public virtual Image IconImage { get; set; }
Now I need to get my image's item name. Is there any way of doing this without making another call to the db?
Get the item name from the image source as the name in the path is always the item name:
Path.GetFileNameWithoutExtension(IconImage.Src)
Related
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();
}
}
How could Image view display photo by URL in SWiftUI?
I have a photo named "0.jpg" in my iPhone, and the URL is "/var/mobile/Containers/Data/Application/EB3DA19A-F7B2-48DD-8681-139C7FCB9090/Documents/Photos".
And my source code is just follows:
var imageSection: some View {
Image(landmark.photoName,bundle: Bundle(url:FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("Photos")))
}
When running, it would get the error:
[SwiftUI] No image named '0.jpg' found in asset catalog for /var/mobile/Containers/Data/Application/E03608D6-24A5-4797-8C16-DDAEB03AB623/Documents/Photos
And I am sure the jpg file is existing, because I use UIImage as parameter and it would display well:
var imageSection: some View {
Image(uiImage: UIImage(named: "/var/mobile/Containers/Data/Application/EB3DA19A-F7B2-48DD-8681-139C7FCB9090/Documents/Photos/0.jpg"))
}
How could I display the image in the Image without UIImage?
Thank you very much!
See documented Image constructor
/// Creates a labeled image usable as content for controls.
///
/// - Parameters:
/// - name: the name of the image resource to lookup, as well as
/// the localization key with which to label the image.
/// - bundle: the bundle to search for the image resource and
/// localization content. If `nil`, uses the main `Bundle`.
/// Defaults to `nil`.
public init(_ name: String, bundle: Bundle? = nil)
This one "name: the name of the image resource to lookup" - not a file name, but image resource name, and all image resources are now in assets catalogs.
For external images there is Image(uiImage:) which you already found - just use it.
I need image field source of a product item to change depending on the folder path I am creating a product item under when creating the item in content or page editor.
If I am creating a product under /home/bicycles, I need the product item image field to auto change to /sitecore/media library/Images/bicycles
If I am creating a product under /home/cars, I need the product item image field to auto change to /sitecore/media library/Images/cars
If I am creating a product under /home/scooters, I need the product item image field to auto change to /sitecore/media library/Images/scooters
The default setting for that image field source in the datatemplate is /sitecore/media library/Images/bicycles
How can I go about doing this?
One option is to create a custom Content Editor field that extends the default image field type.
Start by creating a class that inherits from Sitecore.Shell.Applications.ContentEditor.Image. Then override the OnPreRender method to determine and set the Source property of the image field based on your location criteria/requirements.
See comments in the code below for more information.
public class ContextAwareImageField : Sitecore.Shell.Applications.ContentEditor.Image
{
/// <summary>
/// The ItemID proprety is set by the Content Editor via reflection
/// </summary>
public string ItemID { get; set; }
/// <summary>
/// Override the OnPreRender method.
/// The base OnPreRender method assigns a value to the Source viewstate property and we need to overwrite it.
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Source = GetSource();
}
protected virtual string GetSource()
{
//retrieve and return the computed source value if it has already been set
var contextSource = GetViewStateString("ContextSource");
if (!string.IsNullOrWhiteSpace(contextSource))
return contextSource;
//retrieve the context item (the item containing the image field)
var contextItem = Sitecore.Context.ContentDatabase.GetItem(ItemID);
if (contextItem == null)
return string.Empty;
//determine the source to be used by the media browser
//in this case we're just checking based on parent path, but any logic could be inserted
contextSource = "/sitecore/media library/Images";
switch (contextItem.Parent.Paths.FullPath.ToLowerInvariant())
{
case "/sitecore/content/home/bicycles":
contextSource = "/sitecore/media library/Images/Bicycles";
break;
case "/sitecore/content/home/cars":
contextSource = "/sitecore/media library/Images/Cars";
break;
case "/sitecore/content/home/scooters":
contextSource = "/sitecore/media library/Images/Scooters";
break;
}
//store the computed source value in view bag for later retrieval
SetViewStateString("ContextSource", contextSource);
//return the computed source value
return contextSource;
}
}
Next, perform the following steps:
Login to the Sitecore desktop with administrator rights and use the database icon in the lower right to switch to the Core database.
In the Core database, open the Content Editor then navigate to /sitecore/system/Field Types/Simple Types. There you will find an item representing the Image field type.
Duplicate the Image field type item and rename the duplicated item to something relevant (e.g. Context Aware Image).
Edit the duplicated item
In the Assembly field, provide the name of the assembly file containing your custom image field class (e.g. MyClient.MySite.dll)
In the Class field, provide the name of the custom image field class, including the namespace (e.g. MyClient.MySite.CustomFields.ContextAwareImageField)
Delete the value in the Control field
Save your changes
Switch back to the Master database.
Open the Content Editor, then navigate to a template that should contain your new image field.
Either create a new field in the template and choose your new custom field type in the Type dropdown. Or, change the Type for an existing image field.
Save your template changes.
In the content tree, navigate to an item based on the template above and click the Browse button for the contained image field. The media browser dialog should default to the source location specified by the logic in your custom field.
Note: if you are using a version of Sitecore that contains a SPEAK-based media browser dialog, you will have to switch to Tree View in the dialog (icon in the upper right) in order to see the source location specified by your custom field.
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
In Sitecore I have created a custom field (via this recipe: http://sdn.sitecore.net/Articles/API/Creating%20a%20Composite%20Custom%20Field/Adding%20a%20Custom%20Field%20to%20Sitecore%20Client.aspx)
The field is for use in the content editor.
The custom field has a menuitem attached (the little textbutton rendered just above the field)
The custom field work as expected and the menuitem hooks into code in the custom field class as it should. However, the logic I need to implement for the menuitem requires that I get a reference to the item that is currently being edited by the user in the content editor.
However, to my surprise this doesn't work:
Sitecore.Context.Item
This does not give me the item that the user is currently editing, but instead the "content editor" item, which is always the same.
I would think there would simply be a property of some object in the API, but I can't find it.
If you are following this article then you will have defined a property on your control..
public string ItemID { get; set;}
.. this will be populated with the ID for the item you are editing. You should be able resolve the Item object from this ID using something like:
Sitecore.Data.Database.GetDatabase("master").GetItem(ItemID)
.. if you are just looking just for the value of the field you are editing you can use the base.Value field.
The best place to ask this would be on the developer forum, you should get a good response on there.
Though, taking a look through the Sitecore code, it looks like this might be what you want. Try reading it from the ViewState:
public string ItemID
{
get
{
return base.GetViewStateString("ItemID");
}
set
{
Assert.ArgumentNotNullOrEmpty(value, "value");
base.SetViewStateString("ItemID", value);
}
}
Stephen Pope is right, though there are more properties that you can 'automagiacally' retrieve from Sitecore. Some properties, just like ItemID are put on the field via reflection. There's also the ItemVersion and Source that can be useful for your custom controls.
If you're intereseted, take a peek inside the class Sitecore.Shell.Applications.ContentEditor.EditorFormatter, and especially its method SetProperties:
ReflectionUtil.SetProperty(editor, "ItemID", field.ItemField.Item.ID.ToString());
ReflectionUtil.SetProperty(editor, "ItemVersion", field.ItemField.Item.Version.ToString());