I working on Xamarin project and made custom renderer for my custom control in UWP project. I found how to set the ControlTemplate by using xml code.
XML Way:
var tb = new TextBox(); // or what I do in Xamarin var tb = Control;
var ct = (Controls.ControlTemplate)XamlReader.Load(#"
<ControlTemplate TargetType=""TextBox"" xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Grid>
....
</Grid>
</ControlTemplate>");
tb.Template = ct;
But how I can do the same in code?
var tb = new TextBox(); // or what I do in Xamarin var tb = Control;
var ct = new ControlTemplate();
ct.TargetType = typeof(TextBox);
var grid = new Grid();
ct.VisualTree = grid // This is how it was done in wpf but there is no such option in UWP
tb.Template = ct;
It's not supported in UWP, and I previously found no way to directly set it. As per the MS docs.
ControlTemplate: this is used as the value of the Control.Template
property, which defines the visuals of a control by applying the
template. You almost always define a ControlTemplate as a XAML
resource, using an implicit key TargetType that is the same as a Style
that sets Control.Template with a Setter. You rarely if ever assign a
value for Control.Template directly on a control instance.
Besides possibly delving into reflection, or using the XAMLReader as per your first example, I have never found another way to do it, like you do in WPF.
Related
I have a DataGrid that is made of dynamically by the user. That means each time it runs the columns could and will be very different. Each column is programmatically added for that reason. I need to add some DataTriggers to it so figured this would work:
Style style = new Style();
style.TargetType = typeof(DataGridTextColumn);
DataTrigger tg = new DataTrigger()
{
Binding = new Binding(value),
Value = "bad data"
};
tg.Setters.Add(new Setter()
{
Property = UIElement.VisibilityProperty,
Value = Visibility.Hidden
});
While this gives no errors in the IDE when run it crashes and gives me
'DataGridTextColumn' type must derive from FrameworkElement or FrameworkContentElement.
What is the correct way of adding a DataTrigger to a DataGridTextColumn Programmatically
You need to use typeof(DataGridCell). The trigger should be applied to the Cell itself, not the Column.
I'm developing wp8.1 runtime app.I need to get a textblock control's actual width to do something before a page(or an usercontrol) loaded. In the wp8 app, I can do this with using the code:
var textBlock = new TextBlock();
textBlock.FontSize = 26;
textBlock.Text = "A";
var width = textBlock.ActualWidth;
but, in the wp8.1 runtime app, the code above cannot get actual width, and it always returns 0.
Could someone tell me how to get textblock actual width before page loaded in wp8.1 runtime app?
Thanks!
I'm actually surprise it worked in WP8.0 SL so I did a little test. You're right it does work. To get it to work in WP8.1 runtime, I would add it to a Container then call UpdateLayout like below:
<Grid x:Name="ContentPanel">
<!--- content xaml --->
</Grid>
private void Page_Loaded(object sender, RoutedEventArgs e)
{
TextBlock tb = new TextBlock();
tb.Margin = new Thickness(-2500, -2500, 0, 0); // hide it using margin
tb.FontSize = 26;
tb.Text = "Test Test Test Test";
this.ContentPanel.Children.Add(tb);
tb.UpdateLayout();
var tb_width = tb.ActualWidth;
var tb_height = tb.ActualHeight;
}
I don't think you can get it before the TextBlock is actually loaded.
The thing is that the default value of ActualWidth is 0 and the default value is what you would get if the TextBlock has not yet been loaded or rendered in the UI. Reference and more details about this property can be found here.
You might be able to catch a SizeChanged event of the TextBlock and do something from there.
I used the following code to create data template items:
FrameworkElementFactory textCtrl = new FrameworkElementFactory(typeof(TextBox));
Binding binding1 = new Binding();
string path = "Syncfusion";
binding1.Path = new PropertyPath(path);
textCtrl.SetValue(TextBox.TextProperty,binding1);
Binding binding2 = new Binding();
string path2 = "Text1";
binding2.Path = new PropertyPath(path2);
textCtrl.SetValue(TextBox.NameProperty, binding2);
DataTemplate dt = new DataTemplate();
dt.VisualTree = textCtrl;
How to get the text box from the data template..?
I tried the following links
Link 1
Link 2
But I did not get the things correctly...
I used the below code in xaml
<DataTemplate>
<TextBlock Text="{Binding CellBoundValue}" gridcommon:VisualContainer.WantsMouseInput="False"/>
</DataTemplate>
Can anyone help me on this?
As far as I know, Microsoft does not recommend to use the FrameworkElementFactory, it may get deprecated some times (not sure about this).
But if you want to do it anyway, you must apply your DataTemplate to create instances of controls declared in the DataTemplate. You can do this for example with a ContentControl or an ItemsControl.
Is it possible to attach datatriggers to a style at runtime? I've been through my (non-working) code a few times now and can't seem to find where I've gone wrong.
Here's the method I use to attach the style and trigger:
private void AttachVisibilityTrigger(Control ctrl)
{
Style stl = new System.Windows.Style();
DataTrigger dt = new DataTrigger();
PropertyInfo pi = _entity.GetType().GetProperty(this.SecondaryOptions[ctrl.Name]);
Type controlType = this.GetControlTypeForProperty(ref dt, pi); //gets the control type based on the property name and then sets the value for the DataTrigger for which I want the visibility to be hidden
Binding b = this.GetVisibilityBindingByControlType(controlType); //returns a new Binding with the appropriate Path set that corresponds to the bound property value (e.g IsChecked for CheckBoxes, Text for TextBoxes, SelectedValue for Comboboxes, etc)
b.ElementName = this.SecondaryOptions[ctrl.Name];
dt.Binding = b;
dt.Setters.Add(new Setter(Control.VisibilityProperty, System.Windows.Visibility.Hidden));
stl.Triggers.Add(dt);
ctrl.Style = stl;
}
I'm pretty sure the binding is just broken, i created similar styles in code and they work.
Especially this line looks quite suspicious:
b.ElementName = this.SecondaryOptions[ctrl.Name];
(If you want to bind to the control itself use RelativeSource instead.)
Have you checked the Output window of VisualStudio for binding errors?
I want to produce in code the equivalent of this in XAML:
<TextBlock
Text="Title:"
Width="{Binding FormLabelColumnWidth}"
Style="{DynamicResource FormLabelStyle}"/>
I can do the text and the width, but how do I assign the dynamic resource to the style:
TextBlock tb = new TextBlock();
tb.Text = "Title:";
tb.Width = FormLabelColumnWidth;
tb.Style = ???
You should use FrameworkElement.SetResourceReference if you want true DynamicResource behaviour - ie updating of the target element when the resource changes.
tb.SetResourceReference(Control.StyleProperty, "FormLabelStyle")
You can try:
tb.Style = (Style)FindResource("FormLabelStyle");
Enjoy!
The original question was how to make it Dynamic, which means if the resource changes the control will update. The best answer above used SetResourceReference. For the Xamarin framework this is not available but SetDynamicResource is and it does exactly what the original poster was asking. Simple example
Label title = new Label();
title.Text = "Title";
title.SetDynamicResource(Label.TextColorProperty, "textColor");
title.SetDynamicResource(Label.BackgroundColorProperty, "backgroundColor");
Now calling:
App.Current.Resources["textColor"] = Color.AliceBlue;
App.Current.Resources["backgroundColor"] = Color.BlueViolet;
Causes the properties to change for all controls using the resource this way. This should work for any property.
This should work:
tb.SetValue(Control.StyleProperty, "FormLabelStyle");
Application.Current.Resources.TryGetValue("ResourceKey", out var value)