databinding formatted text to a richtextbox in silverlight 4 - data-binding

I need to databind formatted text to a RichTextBox. For formatting it seems that I will have to create a series of Runs with their specific formats and then add them to a paragraph and add it to the blocks property on the RichTextBox. I tried to bind a paragraph property to Blocks but it doesn't seem to allow that. Paragraph doesn't have an itemsource to bind it to a list of Runs. How can I possibly databind the list of Runs to a RichTextBox Widget ?
Thanks

Here is the solution I came up with. I created a custom RichTextViewer class and inherited from RichTextBox.
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
namespace System.Windows.Controls
{
public class RichTextViewer : RichTextBox
{
public const string RichTextPropertyName = "RichText";
public static readonly DependencyProperty RichTextProperty =
DependencyProperty.Register(RichTextPropertyName,
typeof (string),
typeof (RichTextBox),
new PropertyMetadata(
new PropertyChangedCallback
(RichTextPropertyChanged)));
public RichTextViewer()
{
IsReadOnly = true;
Background = new SolidColorBrush {Opacity = 0};
BorderThickness = new Thickness(0);
}
public string RichText
{
get { return (string) GetValue(RichTextProperty); }
set { SetValue(RichTextProperty, value); }
}
private static void RichTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((RichTextBox) dependencyObject).Blocks.Add(
XamlReader.Load((string) dependencyPropertyChangedEventArgs.NewValue) as Paragraph);
}
}
}

Related

Localization in EnumDropDownListFor using asp.net boilerplate

I have an enum dropdown
//control
#Html.EnumDropDownListFor(
m => m.OrderBy,
new {#class = "btn btn-default dropdown-toggle toggle", onchange = "document.getElementById('hf_Pagename').value,this.form.submit();"})
//my enum
public enum OrderByOptions
{
Default,
PriceLowToHigh,
PriceHighToLow,
MostRecent
}
Now the problem is I need to localize them. But in this case from" PriceLowToHigh" needs to change to " Price- low to high"
You can use AbpDisplayNameAttribute:
public enum OrderByOptions
{
[AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.Default")]
Default,
[AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceLowToHigh")]
PriceLowToHigh,
[AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.PriceHighToLow")]
PriceHighToLow,
[AbpDisplayName(MyConsts.LocalizationSourceName, "OrderByOptions.MostRecent")]
MostRecent
}
Define them in your localization files:
<text name="OrderByOptions.PriceLowToHigh">Price - Low to High</text>
Update
AbpDisplayName works on type class
You can define:
[AttributeUsage(AttributeTargets.Field)]
public class FieldAbpDisplayNameAttribute : AbpDisplayNameAttribute
{
// ...
}
Then use [FieldAbpDisplayNameAttribute(...)] instead.
There are many ways to achieve the issue.
Way #1
Don't use #Html.EnumDropDownListFor! Just traverse enum and create the html element like below;
(I am writing the code on the top of my head)
<select>
#foreach (var item in Enum.GetValues(typeof(OrderByOptions)))
{
<option value="#((int)item)">#(Localize(item.ToString()))</option>
}
</select>
There's no Localize method. Just localize it with your way.
Way #2
Other alternative is not using enum but create a dropdown item collection. And let an item consist of DisplayText and Value. Display Text must be localized from server.
Way #3
Follow the instructions explained here:
https://ruijarimba.wordpress.com/2012/02/17/asp-net-mvc-creating-localized-dropdownlists-for-enums/
With the information above, I solve my problem this way.
I created a custome Attribute AbpEnumDisplayNameAttribute inherited from AbpDisplayNameAttribute.
[AttributeUsage(AttributeTargets.Field)]
public class AbpEnumDisplayNameAttribute : AbpDisplayNameAttribute
{
/// <summary>
/// <see cref="AbpDisplayNameAttribute"/> for enum values.
/// </summary>
public AbpEnumDisplayNameAttribute(string sourceName, string key) : base(sourceName, key)
{
}
}
Then I created an extension for Enum display value localization.
public static class EnumLocalizationExtension
{
public static string ToLocalizedDisplayName(this Enum value)
{
var displayName = value.ToString();
var fieldInfo = value.GetType().GetField(displayName);
if (fieldInfo != null)
{
var attribute = fieldInfo.GetCustomAttributes(typeof(AbpEnumDisplayNameAttribute), true)
.Cast<AbpEnumDisplayNameAttribute>().Single();
if (attribute != null)
{
displayName = attribute.DisplayName;
}
}
return displayName;
}
}

Route PropertyChanged Event from static ObservableCollection to non-static wrapper class

I am trying to bind an ObservableCollection, which is filled in a Background thread, to a charting Control in the UI.
Therefore i have a static class "Core", which have a member "DataState". This DataState class owns the ObservableCollection "SensorData", which is filled in the aforementioned Background Task.
As there is no possibility to do UWP XAML Binding for static properties i wrote a wrapper class "DataWrapper" which is nonstatic and refers to the static Core.DataState.SensorData-object.
Here's some of my Code:
Core.cs:
public static partial class Core
{
private static DataState m_DataState;
public static DataState DataState
{
get
{
return m_DataState;
}
set
{
if (value != null)
{
m_DataState = value;
}
}
}
}
DataState.cs:
public class DataState
{
private ObservableCollection<SensorData> m_SensorData = new ObservableCollection<SensorData>();
public ObservableCollection<SensorData> SensorData
{
get
{
return m_SensorData;
}
set
{
if (value != null)
m_SensorData = value;
}
}
DataWrapper.cs:
public class DataWrapper
{
public ObservableCollection<SensorData> SensorData
{
get
{
return Core.DataState.SensorData;
}
}
}
XAML:
<Charting:LineSeries Name="MySeries" Title="Title" IndependentValuePath="X" DependentValuePath="Y" ItemsSource="{x:Bind DataWrapper.SensorData}"></Charting:LineSeries>
where X and Y are the variables contained in the SensorData-Object.
So, if i wait to Show the Charting-Control until there's some data in the ObservableCollection this data is nicely plotted into my Control. But after that recently added data is not plotted anymore.
Therefore i am looking for a way to route the PropertyChanged-Event from Core.DataState.SensorData somehow to DataWrapper.SensorData. Is there any possibility to do this?
Is the structure of this Problem clear to you? I think my descriptions sounds a bit confusing...
Thank you in advance for any help :-)

DevExpress: Customize the XAF Scheduler List View Display Text

I have a business object in my XAF application which inherits from the standard Scheduler'Event' class. In the list view I get the default scheduler list view where the boxes display the descriptive text. I want to display additional text in those boxes. I looked around and found "ScheduleControl.InitAppointmentDisplayText" event but could not figure out how to implement it within my class.
You can implement the following code within a view controller within the Module.Win project.
namespace Project.Module.Win.Controllers{
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Scheduler.Win;
using DevExpress.XtraScheduler;
public partial class SchedulerViewController : ObjectViewController<ListView, Project.Module.BusinessObjects.Event>
{
public SchedulerViewController()
{
this.InitializeComponent();
this.RegisterActions(this.components);
}
protected override void OnViewControlsCreated()
{
base.OnViewControlsCreated();
SchedulerListEditor listEditor = View.Editor as SchedulerListEditor;
if (listEditor != null)
{
SchedulerControl scheduler = listEditor.SchedulerControl;
if (scheduler != null)
{
scheduler.InitAppointmentDisplayText += new AppointmentDisplayTextEventHandler(this.SchedulerControl_InitAppointmentDisplayText);
}
}
}
private void SchedulerControl_InitAppointmentDisplayText(object sender, AppointmentDisplayTextEventArgs e)
{
MyEventObject myEventObject = this.ObjectSpace.GetObjectByKey<MyEventObject>(e.Appointment.Id);
if (myEventObject != null)
{
e.Text = string.Concat("Text Goes Here - ", myEventObject.FieldValue);
}
}
}

Elegant way to bind html radio buttons <=> Java enums <=> mysql enums in Play?

The Goal is to have a list of options (that a user can chose through radio buttons) in one place(for eg: a yaml config file). No other place should have this list hard-coded
I've done something similar to create select elements, and I think enums worked just fine. Doing radio buttons should be very similar. I've set it up so that the labels can be defined in the messages file. I'm going to try to excerpt the relevant portions from my larger auto-form-generation code (using FastTags) the best I can. It's a bit heavy for this one case but it makes sense in the larger system.
I use the tag like #{form.selector 'order.status' /}, which looks find the variable named order in the template, sees that status is declared as public Status status, and then goes to find all the values of the Status enum and generate options for them in the select element.
First, I use a FieldContext object which just contains a bunch of info that's used by the other code to determine what to generate along with some utility methods:
public class FieldContext {
public final Map<?,?> args;
public final ExecutableTemplate template;
public final int fromLine;
public Class clazz = null;
public Field field = null;
public Object object = null;
public Object value = null;
private Map<String,String> attrs = new HashMap<String,String>();
private Map<String,Boolean> printed = new HashMap<String,Boolean>();
private List<Option> options;
...
Then I have this in another helper class (its info gets added to the FieldContext):
public List<Option> determineOptions(FieldContext context) {
List<Option> options = new ArrayList<Option>();
if (context.field.getType().isEnum()) {
for (Object option : context.field.getType().getEnumConstants()) {
options.add(new Option(option.toString(), Message.get(option.toString())));
}
}
return options;
}
then the tag declaration is
public static void _selector(Map<?,?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
String field_name = args.get("arg").toString();
TagContext.current().data.put("name", field_name);
SelectHelper helper = HelperFactory.getHelper(SelectHelper.class);
try {
FieldContext context = new FieldContext(field_name, args, template, fromLine);
helper.autoconfigure(context);
TagContext.current().data.put("selected", helper.determineValue(context));
out.print("<div class=\"formutil-field formutil-selector\">");
out.print("<label for=\"" + context.getAttr("id") + "\">");
out.print(helper.findOrCreateLabel(context));
out.print("</label>");
out.print("<select");
context.printAttribute(out, "id", "name");
out.print(">");
if (context.hasOptions()) {
for (Option option : context.getOptions()) {
out.print("<option value=\"" + option.value + "\">" + option.label + "</option>");
}
}
out.print("</select>");
context.printErrorIfPresent(out);
context.printValidationHints(out);
out.println("</div>");
}
...
}

Dynamic change of an editor type for ASPxGridView

I have an ASPxGridView from DevExpress fed with data from ObjectDataSource. My data row objects expose properties such ParameterName, ParameterType and ParameterValue.
//Properties, constructor and private fields code omitted for clarity
public class InputParameterDescription
{
public string ParameterName;
public Type ParameterType;
public int ParameterPrecision;
public string ParameterDescription;
}
ParameterValue is always an object of type indicated by ParameterType property. In fact, I use few types – Int32, Double, String or Boolean. When I display values in a grid and user clicks “Edit” a ParameterValue is always edited with TextBox. Is it possible to change editor for this column according to ParameterType? I want my users to use SpinEdit for integers, checkbox for Boolean, etc.
In fact, this is the way people have been working with DevExpress Delphi grids - TdxGrid and TcxGrid (OnGetProperties event). I have asked this question in DevExpress forum, but haven’t got any answer :(
You could create a template on that column that would do the switch for you. Something like:
public class SwitchTemplate : ITemplate
{
public void Instantiate(Control container)
{
GridViewDataItemTemplateContainer cnt = (GridViewDataItemTemplateContainer) container;
switch( GetStringParameterTypeFromDataItem(cnt.DataItem) )
{
case "Int32":
container.Controls.Add( new ASPxSpinEdit() { ... } );
break;
case "DateTime":
container.Controls.Add( new ASPxDateEdit() { ... } );
break;
case "String":
container.Controls.Add( new ASPxTextBox() { ... } );
break;
...
}
}
}
Then you just need to specify this template as the EditItemTemplate of the column:
myGrid.Columns["MyColumnName"].EditItemTemplate = new SwitchTemplate()

Resources