I have an ItemsControl.I want to hide the Text Box in the ItemsControl DataTemplete on some Flag. But when i access the Text Box in Code Behind File .it gives the error doesn't exists in the current context.Is there a way to do this using SilverLight?
Let's go by parts, in case you are not using MVVM, due to you are using a DataTemplate the Control is not added as usual in the visual tree, so you can do the following, add to the TextBox the LoadedEvent, and create a List in order to have all the TextBoxes inside, and there you can do what you need when you want.
In case you are using Binding with MVVM, you can bind the property of that control, like the Text with Mode=TwoWay or the Visibility with a bool and adding a converter.
That is a general answer to your general question, in case you need more details, please add the specific code you are using.
Related
I know that I can set the alternating row colour of list box by defining Item control style like this one
https://stackoverflow.com/a/3567894/1241992
I want to know how can I set this using Blend for Visual Studio. When I am designing my layout in Blend, I do not want to edit code and define my styles that way. I want to visualize and create my styles and I am sure there must be some way of doing it but I cannot figure out how. Strange thing is even if I try to edit this already defined style, I can not create another trigger property which defines AlternationIndex via Blend.
Afraz,
You can do at least part of it using Blend.
In Blend, select the ListBox, right click : Edit Additional templates/ Edit generated item container(itemContainerStyle)/ Edit a copy.
It will create most of the code for the style of the ListBoxItem, that is a line of a ListBox.
You can actually create some triggers going to the Trigger tab in the left of the screen. We want a property trigger
But the triggers can only be created on properties of the ListBoxItem properties.
Only properties of the ListBoxItem can be source of the triggers in the ComboBox just under.
So rest of the code to affect the background color of the ListBoxItem has to be done in code.
Regards
I'm in the process of learning WPF and currently exploring data binding. I have a DataGrid control on my form, and in my C# code for the form I have a List<string> variable.
I want to be able to use the Properties UI for the DataGrid in the designer to bind the List<string> to the DataGrid. I cannot figure out what I need to do or where I need to look in the UI to do this.
This is what I am doing:
Click my DataGrid in the UI designer to make it the active control.
Move to the Properties window.
Scroll down to the ItemsSource property.
Click on the field and the UI with Source, Path, Converter and Options pops up.
And when I get to this point I no longer know what to do.
I do not want to accomplish this by writing/modifying XAML. I want to know how it works using the UI.
Having never used the designer before, I can't be totally sure (your use case isn't quite clear either).
That being said, in my designer you
Set the "Binding Type" to "Data Context"
Select the "Custom" text box (needed for me because it doesn't see my DataContext)
Type the name of your property in the "Path" field (you can only bind to Properties)
Hit OK.
Note that this is the same as writing in XAML:
<DataGrid ItemsSource="{Binding MyItemCollection}"/>
<!-- or --!>
<DataGrid ItemsSource="{Binding Path=MytItemsCollection}"/>
There's a reason no one uses the designer....
The other options are more "advanced" binding concepts that you don't normally use on ItemsSource properties.
Note that DataGrid is a poor choice for displaying strings. ListView or ListBox are much better choices, as they don't assume your information has multiple pieces (like DataGrid does).
I understand not liking XAML, as it really intimidated me at first, but I will quickly say that it is a powerful tool. I am not sure how to do it through the designer, but in C# let's say you name your DataGrid 'myDataGrid' and your List is named 'stringList'. It is as simple as the following:
myDataGrid.ItemsSource = stringList;
and the data grid is now bound to your string list.
Thanks for asking the question! The properties window is so underrated.
First you must set the DataContext.
It's in the common section of the properties window. Set the data context to whatever view model you need. If you don't have a VM and the List is in the code behind, set the data context to relative source self.
Next in the Path write the name of your List.
Also, you may want to use ObservableCollection instead of List so your objects are updated in the UI as they change.
In my WPF application, my Viewmodel has a boolean property IsOwnerOf and a string property Title. If IsOwner==false, I want a TextBlock displaying the Title (because if you're not the owner, you should not be able to edit it) and if IsOwner==true, I want a TextBox displaying Title - obviously at the same place in the view.
Also I don't want to do it codebehind since I follow the MVVM pattern. Thought about Style.Triggers, but with them I can only influence attributes of an element, not the element type itself, or can I?
EDIT:
Practically the answers below regarding triggering Visibility or IsReadOnly work, but I still would like to see a conceptually better answer! What if I replace the TextBox resp. TextBlock by elements that don't have these convenient properties? There must be a better way than creating both and hiding one of them, that just doesn't sound right...
The easiest option is to always drop a TextBox and bind it's IsEnabled or IsReadOnly property to the IsOwner flag.
You can also use a DataTemplateSelector to achieve this.
You can use triggers to change the Visibility of your TextBlock and TextBox using a BooleanToVisibilityConverter
I want the controls in my wpf window to change depending on whether the user is viewing or editing/inserting data. For example, I want to display a label at view time, but a text box (or combo box etc) at edit/insert time.
Can I do this with DataTemplates or do I have to have two controls for each data item and change visibility depending on what mode the form is in?
If DataTemplates will do the job, can anybody point me to some examples?
many thanks
mcalex
In my opinion, the better way to do this is using differrent data templates for control, in case when you always need to use them together.
In any case you have to select switch templates or controls mechanism. You can use DataTrigger (first answer) or ValueConverter
I'm checking this page and I like the only answer there is.
Editable WPF treeview item on doubleclick? (with styles?)
But I don't like the idea to use c# lines, Might there be a possible to write that code using XAML?
You can use EventSetter, or AttachedBehavior. But they are assumes that you need c# code-behind.
How to do it in XAML - hard to say. One more possible way - to use x:Code clause to write c# code inside xaml.
You can use CheckBox in the XAML and using its IsChecked property change the Style to the Editable mode