How can I change properties of some controls in DataTemplate that placed in resource from code?
I use .NET 3.5, so DataTemplate doesn't have Template property.
Take a look at DataTemplateSelectors
--Edit--
I'm sorry I did not understand the question at first, I think the best way would be to write an extra template, that looks the way you want to modify the other one, find it and assign it
to the list control.
Here is a little example
Related
This may seem familiar to you but I have a problem creating a custom List Box with custom style as well. I can only do one of them at the same time... I can't use both. and another thing is that could you tell me how to add the custom listboxitem in c# code so I could easily add it to the program?
what I mean is that for example if you put an image before the text of listboxitem and then give it an style, but all thease must be done for all items.
thanks to all
First, what have you already tried out? Are you expecting SO users do the work for you?
Listbox tag has a ListboxItemTemplate. For that you can implement a DataTemplate has a StaticResource and put whatever you want inside.
Check the documentation on MSDN.
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate(v=vs.110).aspx
I have a template assigned to a button:
<Button x:Name="btn_PatMat" Template="{StaticResource PatMat_Button}" ...
How can I retrieve the Key/String/Name of this template from said button?
Pseudocode:
String = btn_PatMat.Template.???.ToString()
You can't. At least not in the way you're trying.
To quote from this SO post about x:Key (emphasis mine):
x:Key is used for items that are being added as values to a
dictionary, most often for styles and other resources that are being
added to a ResourceDictionary. When setting the x:Key attribute,
there is actually no corresponding property on the object or even an
attached dependency property being set. It is simply used by the
XAML processor to know what key to use when calling Dictionary.Add.
StaticResources are evaluated during loading, so once the control loads, the Template property is no longer set to a binding, but is instead set to a copy of the ControlTemplate from your Resources, and no corresponding property on that object is set to the key.
You can verify this by checking out the XAML of the button after it's loaded, by using something like XamlWriter.Save to view it's XAML string.
The only solution I can think of that might work would be to loop through your .Resources, and find a ControlTemplate that is equal to your Button's ControlTemplate. I haven't tested this, and it probably isn't very practical for large resource libraries, but it may be an option.
But a better solution would probably be to change your logic so the key value can be accessed some other way by whatever object needs it.
Well I'm afraid that's not possible because it's not intended by WPF. There are some people which wanted to get access to x:Name which is similar to x:Key, they all had to give up.
Pls have a look at this SO post and this additional link.
The only workaround I could imagine is reading all templates from the ResourceDictionary, instantiate each resource (if possible), find the template (if it's e.g. a style) and compare it with the current instance of the template found in the control. But this seems to be a pretty ugly solution and I'm not sure if it'll work without any problems.
This is in C# in WPF:
I know I can add items to a stack panel like so: myStackPanel.Children.Add(new Button());
Or to a ListBox like so: myListBox.Items.Add(new Button());
Of course, I could edit beforehand the controls and add them latter, like set the proprieties first then add them.
But how do I select the control once it is in the stack layout with code behind. For example, is there a way similar to this: myStackPanel.Childern.CONTROL_AT_INDEX[n] ? And then how can I edit it even more like change the content of a label if it is a label or the event handler if it is a button ?
Also I want a solution for the ListBox as well please.
I just don't know how to access those controls once they are inside.
Assign to that controls x:Name and use that in your code behind.
This is naturally not valid for controls present in Templates and Styles.
Like Tigran already posted, is possible to assign an attribute to your controls in XAML:
<ListBox x:Name="myListBox"
// more properties here...
/>
Then your code-behind will then be able to compile your line:
myListBox.Items.Add(new Button());
However, I strongly suggest you to alternativly use a MVVM approach to get rid of code-behind files. This reduces coupling of your business logic from the UI. Using the MVVM pattern is Microsoft's recommended way for working with WPF as it makes using many WPF feature very easy.
A great resource for Tutorials can be found int this SO thread, for example: MVVM: Tutorial from start to finish?
Here is my solution
var child = (from c in theCanvas.Children
where "someId".Equals(c.Tag)
select c).First();
Is XAML really necessary? If someone really wanted to, could everything displayed on a Silverlight page be declared at runtime in the C# code?
VISIFire has example code where they set up one of their classes, chart, in the C# code and then simply add it to the user interface with a simple C# call:
LayoutRoot.Children.Add(chart);
How would I do a similar creation with a class I already have defined in the XAML file but because of other reasons I need to declare it at runtime? If I could use it as it is in XAML I think it would work because there are no errors with its display. It is declared like this:
<views:PrescriberPage01 x:Name="DashboardPrescriberPage01"
Visibility="Collapsed" SelectedProduct="{Binding SelectedProduct,
Mode=TwoWay}"Grid.Row ="0"/>
How would I declare that in C# code?
Is XAML really necessary?
I suppose no, you can probably do everything that you can do in XAML in code. But XAML is really, really a much, much more convenient way to go. And I think few would dissagree with this.
How would I declare that in C# code?
You can create bindings in code. Example from MSDN. In your situation it would be something like:
PrescriberPage01 page = new PrescriberPage01();
page.Visibility = Visibility.Collapsed;
Binding myBinding = new Binding("SelectedProduct");
myBinding.Mode = BindingMode.TwoWay;
myBinding.Source = this; // or whatever object carrying the property
// "SelectedProduct" that you bind against.
page.SetBinding(PrescriberPage01.SelectedProduct, myBinding);
LayoutRoot.Children.Add(page);
XAML is preferable to describe user interface, in declarative way and I recommend use XAML without code behind and also I would recommend look at MVVM pattern
I would rather say "Is C# really necessary for GUIs when I can use XAML" ? XAML is really good and was explicitly design for GUI.
You have two options here. Load your XAML dynamicly.
Or create it in code. (other posted the solution while i was typing AGAIN! ;) )
Simple controls are easy but... you might also want some styling done to those objects +
you might want an animation or two... this is already(mostly) provided in xaml ... and it is uber easy... If u hate xaml so much you might want to think about "Blend";p
One specific example of something you can't create without Xaml is a DataTemplate.
You can write code which will create a DataTemplate from a XAML string, but you can't add children to a newed up DataTemplate directly in c#.
I've got an ObservableCollection of POCOs (Plain old CLR Objects) that I want to represent in a tabbed idiom. Prefereably using the MVVM pattern, is there a way to bind the collection of TabItems to the count of my POCO collection?
So, in this case if there are 3 items in my collection, I'd like to see 3 TabItems. Each TabItem would contain the same controls in the same location, each control bound to properties of the appropriate object in the collection.
I'm just looking for an overview of the approach I might use or a link to an example. If you need more info, feel free to ask.
Thanks.
I'd probably create an ObservableColletion with your POCO items in it. You could then bind that ObservableCollection to any of the Silverlight Item Rendering controls. You'll have to modify the default rendering template to create your tabs...but using that method, your tabs will constantly be up to date with the items in the collection without having to put any code in the code behind file.
UPDATE
Here's a link to the Silverlight Forums where somebody built a TabControl using the ItemsControl with sample XAML code:
http://silverlight.net/forums/t/12271.aspx
...just scroll down a bit to see the sample.
One way to do this is to use a value converter (IValueConverter) to return your POCO wrapped in a TabItem. I posted an example here as part of a related question. There is also sample xaml binding and injection of the ViewModel as a parameter to the value converter.
/jhd