This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special symbols in WPF binding - what does “{Binding Path=.}” mean?
I am not getting the meaning of following code -
<DataTemplate>
<Label>
<Hyperlink Command="{Binding Path=.}">
<TextBlock Text="{Binding Path=Header}"/>
</Hyperlink>
</Label>
</DataTemplate>
Can anyone explain it.
It is used to bind to the current source .
For more info take a look here Binding Path
hope this helps
Binding basically you are going to binding to ViewModel class. For e.g. you have an class called person and it has property called "Name". What you need to do here is, So it will bind to your Class called Person and it's property called "Name".
<TextBlock Text="{Binding Path=Name}"/>
Related
I am new to WPF and I tried the code below :
<Grid>
<Label x:Name="two" Content="text"/>
<Label x:Name="one" Content="{Binding ElementName=two}"/>
</Grid>
I got error saying
Specified element is already the logical child of another element
From googling I understood that I have a control which is already a child to some control and I am trying to assign it to another one as its child.
Which I did not figure it out in my code.
What may cause the problem ?
Thank you in advance.
You can bind to the Content property of two:
<Label x:Name="one" Content="{Binding Content, ElementName=two}"/>
But a visual element can only appear once in the visual tree, so you can't bind the Content property to the Label itself.
By the way, if you want to display text, you should prefer TextBlocks over Labels:
<TextBlock x:Name="two" Text="text"/>
<TextBlock x:Name="one" Text="{Binding Text, ElementName=two}"/>
I have an xceed:DataGridControl with bounded ItemsSource. Currently I'm trying to set my in/visible columns and the title/headertext for each visible column. Preferably I would like to bind a property in my ViewModel, to set the in/visible columns and theirs titles. But I find no way I could do it. Does anyone know a solution to this problem?
<xceed:DataGridControl
x:Name="dataGridControl"
SelectedItem="{Binding SelectedTextItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding ItemsSourceData, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
</xceed:DataGridControl>
Yes indeed I had to deal with xceed's controls few months ago.
DataGridControl allows you to generate columns automantically. That is also its default behavior.
In order to have your own columns you will have to disable the property AutoCreateColumns and futhermore you will have to set few columns on the property DataGridControl.Columns.
There you will be able to bind Visible property of the Column.
Thanks to Peter for providing this code:
<xceed:DataGridControl ItemsSource="{Binding TextSet}" >
<xceed:DataGridControl.Columns>
<xceed:Column FieldName="ColumnId" Title="{Binding DatagridTitle[ColumnId], Mode=OneWay}" Visible="True" />
</xceed:DataGridControl.Columns>
</xceed:DataGridControl>
I also met similar issue.
You can use the Visible property, then do following:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type typeOfAncestor}}, Path=DataGridControl.DataContext.BooleanSourceProperty}"/>
For example, if the typeOfAncestor is xcdg:MergedColumn and BooleanSourceProperty is IsVisble, then the code should be:
<xcdg:ColumnFieldName="ColumnId" Title="ColumnId"
Visible="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type xcdg:MergedColumn}}, Path=DataGridControl.DataContext.IsVisible}"/>
Then the issue can be solved.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
get value of checkbox from datagrid? C#
I am trying to find a control inside the selected row in a templated DataGrid.
<DataGridTemplateColumn Header="Local">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkImport" IsChecked="{Binding IsLocalized}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I am trying the following code:
var selectedRow = (DataGridRow) gridFileScan.ItemContainerGenerator.ContainerFromItem(gridFileScan.SelectedItem);
CheckBox chkImport = FindVisualChild<CheckBox>(selectedRow);
but chkImport is always null. Any ideas ??
When you debug you should be able to see the method recurse the VisualTree.
You can see the Visual Tree by using the Visual Tree Visualizer
The implementation of FindVisualChild might be flawed or the VisualTree doesn't look like what you expect.
Found it. I just had to call this method after modifying ItemsSource:
gridFileScan.UpdateLayout();
In my WPF project, I have a ListBox that displays items from a List<string> collection. I wanted to make the text of these items editable, so I wrapped each of them in an ItemTemplate with a TextBox (might not be the best way, but I'm new to WPF). I was having trouble simply binding the TextBoxes' Text property to the value of each item. I finally stumbled upon an example using a single dot or period for its Path property ({Binding Path=.}):
<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
However I don't understand why simply using {Binding} didn't work.
It raised a "Two-way binding requires Path or XPath" exception, as according to Microsoft:
[...] a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}"
Could someone shed light on this ambiguous behavior?
EDIT: Moreover, it seems {Binding Path=.} does not necessarily give two-way binding, as modifying the text and moving the focus does not update the underlying source (the same source has also properties displayed and successfully modified on a DataGrid control). I'm definitely missing something here.
The point of the exception presumably is that you cannot two-way bind a binding-source itself, so it tries to prevent you from creating a binding which does not behave the way you would want it to. By using {Binding Path=.} you just trick the error detection.
(Also it's not unheard of that documentation is erroneous or inaccurate, though i do like the MSDN documentation a lot in general as it usually does contain the crucial points one is interested in)
The documentation states that {Binding} is equivalent to {Binding Path=.}. However it is not equivalent to {Binding Path} as you have typed. If you include the Path property, you must assign it to something, be it Path=. or Path=OtherProperty.
These are not the same. If you bind this where ConsoleMessages is an ObservableCollection string with just {Binding} you get a "Two-way binding requires Path or XPath." exception where as {Binding Path=.} works. This is with WPF 4.0...
<ItemsControl x:Name="ConsoleOutput" ItemsSource="{Binding ConsoleMessages, Mode=OneWay}" MaxHeight="400">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=.}" BorderThickness="0" Margin="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My 2p worth...
In short, the difference between the two is analogous with the difference between the traditional pass by value and pass by reference. (FYR - What's the difference between passing by reference vs. passing by value?)
However I don't understand why simply using {Binding} didn't work (it raised a "Two-way binding requires Path or XPath" exception)
Lets assume here for now that {Binding} can be used for two way binding. In general {Binding} creates a value based link with datacontext which does not allow updating the datacontext.
Whereas {Binding Path=.} creates reference based link with the memory area referenced by the 'Path' which allows updating the value through reference.(in this case 'dot' the current datacontext).
Hope this helps!
Adding an x:Name attribute to a XAML element normally results in a member variable being added to the backing class that can then be accessed using normal code. When the element in question is part of the DataTemplate, the field does not get created.
I can sort of understand that the DataTemplate is making this a special case but can anyone explain the underlying principle to me? Also what are the options for getting access to the object within .NET Code?
<dataControls:DataForm x:Name="CompanyDetail" CurrentItem="{Binding CurrentItem}" AutoGenerateFields="False">
<dataControls:DataForm.EditTemplate>
<DataTemplate>
<StackPanel dataControls:DataField.IsFieldGroup="True">
<dataControls:DataField Label="About">
<Border Height="150" Style="{StaticResource HtmlPlaceHolderBorderStyle}" Width="298" VerticalAlignment="Top">
<telerik:RadHtmlPlaceholder x:Name="uxAboutHtml" x:FieldModifier="Public" HtmlSource="{Binding About, Mode=TwoWay}"/>
</Border>
</dataControls:DataField>
</StackPanel>
</DataTemplate>
</dataControls:DataForm.EditTemplate>
</dataControls:DataForm>
You can use the FrameworkElement.FindName("objectName") method on the parent of the DataTemplate e.g. var uxAboutHtml = CompanyDetail.FindName("uxAboutHtml"); to get a reference to the object. The downside to this is that paramater passed to FindName does not end up being strongly typed with the XAML x:Name"objectName" attribute.
I have changed tack on this and am now referencing the underlying object that the control is being bound to, which is probably a better way to go.
var htmlContent = (CompanyViewModel)CompanyDetail.CurrentItem;