How to create a DataTemplate with code?
DataTemplate does not have a VisualTree property like in WPF.
The docu doesn't help either.
There is an IDataTemplateController, but it does not control anything. <ignorable>weird MAUI times again</ignorable>.
Found it in source code: There is a constructor parameter for a function which creates the view.
/// <Docs>
/// <param name="loadTemplate">A custom content generator to be called </param>
/// <summary>Creates and initializes a new instance of the <see cref="T:Microsoft.Maui.Controls.DataTemplate" /> class.</summary>
/// <remarks>To be added.</remarks>
/// </Docs>
public DataTemplate(Func<object> loadTemplate);
new DataTemplate(() => {
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("."));
return label;
});
There is currently a bug in Binding and you have to specify ".", a fix is merged.
Related
I am trying to reuse NumberBoxes for a GridView because having the NumberBoxes embedded directly in the GridView data template causes undesirable behavior, while reusing them does not. The problem is that I keep getting exceptions. They say "No installed components were detected" on the following line (templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox; in this context
/// <summary>
/// The callback for updating a container in the GridView named CardGridView.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void UpdateGridViewContainer(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.Phase == 1)
{
Grid templateRoot = args.ItemContainer.ContentTemplateRoot as Grid;
CardItem item = args.Item as CardItem;
(templateRoot.FindName("NumberBox") as GridViewItem).Content = item.NumberBox;
TypedEventHandler<NumberBox, NumberBoxValueChangedEventArgs> handler =
(box, args) =>
{
if (!double.IsNaN(args.NewValue))
{
_viewModel.ChangeCount(args, item);
}
};
item.SetHandler(handler);
}
}
The exception is thrown when the Page that contains the GridView is left and renavigated to. I have tried nulling out the NumberBoxes when the page is left, but that did not work. Well, it appeared to before the issue cropped up again.
This is the code that nulls out the NumberBoxes
/// <summary>
/// Creates new NumberBoxes for when this Page is loaded again.
/// </summary>
private void ResetNumberBoxes()
{
foreach (CardItem card in CardGridView.Items.Cast<CardItem>())
{
card.ResetNumberBox();
}
CardGridView.ItemsSource = null;
CardGridView.Items.Clear();
}
ResetNumberBox is just setting the NumberBox to null and assigning a new one.
The exception details
System.Runtime.InteropServices.COMException
HResult=0x800F1000
Message=No installed components were detected. (0x800F1000)
Source=WinRT.Runtime
StackTrace:
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.IContentControlMethods.set_Content(IObjectReference _obj, Object value)
An update. I have removed the GridViewItem control from the DataTemplate and tried doing the following with the same result
CardItem item = args.Item as CardItem;
Grid.SetColumn(item.NumberBox, 1);
item.NumberBox.HorizontalAlignment = HorizontalAlignment.Center;
item.NumberBox.VerticalAlignment = VerticalAlignment.Center;
(templateRoot.FindName("GridViewTemplate") as Grid).Children.Add(item.NumberBox);
I also examined a heap dump right before the line that throws, and there was only one instance of the Grid, and the NumberBox had a parent of null.
I have a message extension for MS Teams (based on Bot Framework v3). When I create a ThumbnailCard and return it to user, Teams inserts name and logo of my app to card. Can I remove them somehow?
This is how I create a card
var card = new ThumbnailCard { Text = "some text", Title= "title"};
Here is a screenshot:
No, the attribution on cards created by a messaging extension can't be removed by the app. Teams will automatically show it.
The ThumbnailCard object has the constructor:
/// <summary>
/// Initializes a new instance of the ThumbnailCard class.
/// </summary>
/// <param name="title">Title of the card</param>
/// <param name="subtitle">Subtitle of the card</param>
/// <param name="text">Text for the card</param>
/// <param name="images">Array of images for the card</param>
/// <param name="buttons">Set of actions applicable to the current
/// card</param>
/// <param name="tap">This action will be activated when user taps on
/// the card itself</param>
public ThumbnailCard(string title = default(string), string subtitle = default(string), string text = default(string), IList<CardImage> images = default(IList<CardImage>), IList<CardAction> buttons = default(IList<CardAction>), CardAction tap = default(CardAction))
{
Title = title;
Subtitle = subtitle;
Text = text;
Images = images;
Buttons = buttons;
Tap = tap;
CustomInit();
}
Have you tried with images = new List<CardImage>() ? same thing for buttons
I know i can use < para /> to create multiline function comments
/// <summary>
/// updates the car scrolling.<para />
/// there are also colors
/// <para />while clearall will remove all </summary>
/// <param name="status">optional to add a new car </param>
public void UpdateDrawing(int updatestatus = bgcar.color.none)
but that behaviour isnt like a linebreak \n it rather behaves like a double line brake ( \n\n ). Is there a way to have only a single line break.
So textblocks could be created for intellisence
Try this:
/// <summary>
/// <para>updates the car scrolling.</para>
/// <para>there are also colors</para>
/// <para>while clearall will remove all</para>
/// </summary>
I have a problem with databinding in a winforms-application.
In the following code i have a databinding to the enabled-property of a textbox. The enabled-state depends on the value of a checkbox.
tbAmount.DataBindings.Add("Enabled", checkBox, "Checked",
false, DataSourceUpdateMode.OnPropertyChanged);
in this code the textbox is enabled if the checkbox is checked. but i need it inverted. i want the textbox to be enabled if the checkbox is unchecked. How can i achieve this?
This should do it.
Binding bind = new Binding("Enabled", checkBox, "Checked");
bind.Format +=
(sender, e) =>
e.Value = !((bool)e.Value); // invert the checked value
textBox.DataBindings.Add(bind);
I know this is a very old post, but I have looked for something similar many times over the years and was never really happy with what I ended up using. Mike Park's answer is great, not only because it works, but because of how simple it is.
All I did was take Mike's answer and turn it into a Control extension. Thanks Mike!
Depending on where and how you use it, you may need to add a reference to System.Windows.Forms and a using System.Windows.Forms statement.
/// <summary>
/// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
///
/// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
/// // Defaults to Control Enabled property.
/// // Always bound to the DataSource Checked property.
/// YourButton.DataBindings.Add(YourButton.UncheckedBinding(YourCheckBox));
///
/// var binding = YourButton.UncheckedBinding(YourCheckBox);
/// YourButton.DataBindings.Add(binding);
///
/// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
/// From: Databinding Enabled if false
/// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
/// </summary>
/// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
/// <param name="control">The control that will consume the DataBinding.</param>
/// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
/// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
public static Binding UncheckedBinding<T>(this Control control, T DataSource) where T : ButtonBase
{
return UncheckedBinding(control, "Enabled", DataSource);
}
/// <summary>
/// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
///
/// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
/// // Always bound to the DataSource Checked property.
/// YourButton.DataBindings.Add(YourButton.UncheckedBinding("Enabled", YourCheckBox));
///
/// var binding = YourButton.UncheckedBinding(YourCheckBox);
/// YourButton.DataBindings.Add(binding);
///
/// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
/// From: Databinding Enabled if false
/// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
/// </summary>
/// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
/// <param name="control">The control that will consume the DataBinding.</param>
/// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
/// <param name="PropertyName">The name of the property that is being bound.</param>
/// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
public static Binding UncheckedBinding<T>(this Control control, string PropertyName, T DataSource) where T : ButtonBase
{
var binding = new Binding(PropertyName, DataSource, "Checked");
binding.Format += (sender, e) => e.Value = !((bool)e.Value);
return binding;
}
I am attempting to localize the DatePicker and TimePicker associated with the toolkit on WP7, but I am unsure of how to access the Header and the application bar texts. I have not been able to find any links showing methods of accomplishing these tasks. Are there any useful links or does someone have the source of how this many be accomplished?
Easiest method to download Source and Samples of the latest version of the Toolkit (Nov 2011) which has by default localization for DatePicker and TimePicker.
Add the it as project reference to your solution.
If you have the Toolkit version prior to Nov 2011,
Again add it as Project Reference in your Solution
In side the project tookit in your solution. Add the necessary resx files. You can see there is a default Resources.resx file which has the English text for date pickers. Add the necessary resx files for other languages.
It's very simple: Parameter - Language. Xaml code:
<toolkit:DatePicker Language="ru-RU" Margin="-12, 0" Value="{Binding BirthDate, Mode=TwoWay}" />
Another alternative without modifying the XAML source is to modify the "HeaderTitle" TextBlock once the page loads.
/// <summary>
/// Called from app.xaml.cs if the user navigates to the DatePickerPage
/// </summary>
/// <param name="page">The page.</param>
public static void DatePickerHook(PhoneApplicationPage page)
{
// Somehow modify the text on the top of the page...
LoopThroughControls(page, (ui => {
var tb = ui as TextBlock;
if (tb != null && tb.Name == "HeaderTitle")
{
tb.Text = "<<Local Translation>>";
}
}));
}
/// <summary>
/// Applies an action to every element on a page
/// </summary>
/// <param name="parent">The parent.</param>
/// <param name="modifier">The modifier.</param>
private static void LoopThroughControls(UIElement parent, Action<UIElement> modifier)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
modifier(child);
LoopThroughControls(child, modifier);
}
}
return;
}
Here's the link to the blog post that describes the modifications to app.xaml.cs also: http://blog.dotnetframework.org/2015/11/09/localise-datepicker-in-wp8-silverlighttoolkit-using-hooks/