WPF, reference to object used in XAML - wpf

I am new to WPF, please do not blame me ;)
I have the following part in XAML:
<ListView x:Name="listView" IsEnabled="{Binding PropertiesEnabled}" Margin="0"
BorderThickness="0" DragDrop1:DropTargetBehavior.IsDropTarget="true"
MinHeight="300" DragDrop1:DropTargetBehavior.DropType="{x:Type UIP:DataItemViewModel}"
ItemsSource="{Binding dataItemCollection}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
AllowDrop="True" KeyDown="listView_KeyDown" MouseUp="listView_MouseUp"
UseLayoutRounding="False" VerticalContentAlignment="Center"
MaxWidth="Infinity" MaxHeight="1000" Grid.Column="1"
HorizontalContentAlignment="Stretch">
In the code behind for the view class, I am able to access listView and most of its properties and attributes like IsEnabled, Margin, ItemsSource etc. However, when it comes to DragDrop1, it is not listed.
The reason why I need to get reference to DragDrop1, which is of type DropTargetBehavior, because some events should be fired programmatically.
What could be the cause of missing DragDrop1? How to I get reference to it?
Cheers.

The property is an AttachedProperty, so it doesn't actually exist as part of the ListView object. You need to use the AttachedProperty class name to access the value, and pass it the ListView as a parameter to get or set the value.
You can get it using DragDrop1.DropTargetBehavior.GetIsDropTarget(listView) or set it using DragDrop1.DropTargetBehavior.SetIsDropTarget(listView, yourValue)

Related

AutoCompleteBox in wpf toolkit display items in "rhino.commom.DaataContract.ClassName" form

<WPFToolkit:AutoCompleteBox
x:Name="atbParentGroup"
IsTextCompletionEnabled="True"
FilterMode="ContainsOrdinal"
ItemsSource="{Binding DataContext.Data.ParentProductGroupList}"
SelectedItem="{Binding DataContext.Data.SelectedParentProductGroup}"
ValueMemberPath="ParentGroupName"
Text="{Binding ParentGroupName}"
Width="120"
HorizontalAlignment="Left" VerticalAlignment="Top"
Grid.Row="0" Grid.Column="1" Margin="5,5,0,0" />
Now I got it. Ensure that the property ParentGroupName you have set as ValueMemberPath delivers a nice result - e.g. a string.
If that is not the case you can provide the full path to the property you want to display:
ValueMemberPath="ParentGroupName.FullName"
or something like that.
Another possibility would be to override the .ToString()-method of the ParentGroupName to return something readable.
Additionally make sure the Text-property does not conflict with the others, if in doubt, don't bind it at all. The SelectedItem-property will do its job.

How to define DataContext in my Resource File or under Window.Resource tag?

I have this binding:
<Window x:Name="_local">
<TextBox x:Name="txtVendorName" DataContext="{Binding ElementName=_this, Path=VendorObject}" Width="200" Height="50" BorderBrush="Black" Text="{Binding Path=VendorName}" />
if i have 50 textboxes and label which I need to bind, do I need to put DataContext for all??
Is there a way I can centralize this DataContext and only define Text/Content for my objects?
I don't want to define DatContext to my Grid. So how can I define DataContext in my?
<Window.Resources></Window.Resources>
If I am guessing your actual requirement right, then what you want is all 50 labels and textblocks should be bound to the data context which is VendorObject from _this object. Although I couldnt really guess what _this object is.
Well DataContext is an Inherited Dependency Property (IDP) what they mean is when a visual parent is set with a data context its data context is acquired by all the children below that visual.
So if you put some Panel in your window and set its data context once with your binding DataContext="{Binding ElementName=_this, Path=VendorObject}" and then put all those 50-100 textblocks and labels under that panel then they will automatically acquire that vendor-object as the data context.
So
<StackPanel DataContext="{Binding ElementName=_this, Path=VendorObject}">
<TextBlock Text="{Binding Path=VendorName}" />
<TextBlock Text="{Binding Path=VendorId}" />
<TextBlock Text="{Binding Path=VendorLocation}" />
.... <!--Put all the 50 UI items here to use same data context -->
</StackPanel>
I hope I guessed correctly of what you need.
Now answering you next question,
So how can I define DataContext in my?
<Window.Resources></Window.Resources>
You cant. But you can create an instance of VendorObject class and give it a resource Key, but thats simply a bad design.

WPF XAML Binding

Hello
I have a problem with a binding that I want to do and can't find any information on how to do it.
Basically I want to bind an object to a property of another object.
For example
<TextBox Text="test" Tag="{Binding ElementName=TxtBx2}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>
This is kind of weird but it would help on the code that i'm implementing, so on a property of one object I want to have another object bind in xaml.
I don't know if this is possible, any pointers would be helpful
Thanks, Ruben
That is how you do it; you just need to specify the Path.
<TextBox Text="test" Tag="{Binding ElementName=TxtBx2, Path=Text}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>
If you are wanting the DataContext of the TextBox; then your Path would change accordingly.
<TextBox Text="test" Tag="{Binding ElementName=TxtBx2, Path=DataContext}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>
If you are needing to use the Tag property within a WPF application you might want to re-evaluate your approach as I have yet to use the Tag property since moving from WinForms as that need has been replaced by leveraging the data binding functionality within WPF.
UPDATE:
If your goal is to bind to a given control versus a property on the control; then don't specify the property name within the Path.
Based on your goal; attached behaviors would be a better approach and allow you to wrap the functionality within the extended DataGrid.
<TextBox Text="test" Tag="{Binding ElementName=TxtBx2,Path=Text}" x:Name="TxtBx1"/>
<TextBox Text="test" x:Name="TxtBx2"/>
Assuming you want the value of the Text property of TxtBx1 to be the value of the Text property in TxtBx2, you would use:
<TextBox x:Name="TxtBx1" Text="{Binding ElementName=TxtBx2, Path=Text}" />
<TextBox x:Name="TxtBx2" Text="test" />
Update
Assuming (possibly incorrectly again!) that you want to bind the TxtBx1 element to the Tag of TxtBx2, you would use:
<TextBox x:Name="TxtBx1" Text="test" />
<TextBox x:Name="TxtBx2" Tag="{Binding ElementName=TxtBx1}" Text="test" />
Just out of interest, why do you want to do such a thing?
Update 2
Assuming that you have a Datagrid that you've extended from the wpftoolkit datagrid and a user control that is a pager for that Datagrid, and when you move to another page you need to do some processing on the datagrid, then why don't you just either update the datagrid in your page change event (if using code behind), or update the items that the datagrid is bound to in your page change verb on your view model (if using MVVM)?
Well, it does make sense to bind to an entire object(not to any specific property) with items control such as this:
<ListBox x:Name="pictureBox"
ItemsSource=”{Binding Source={StaticResource photos}}" …>
......
</ListBox>

WPF: ComboBox with selecteditem set make not use of SelectedIndex=0?

Why is the first element in my combobox popup menu not shown in the selected item area of
my combobox , when I use the SelectedItem binding? Without that it is showing up ?? Using
the same code selecteditem + selectedindex that is no problem!
<ComboBox
ItemsSource="{Binding SchoolclassSubjectViewModels}"
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel}"
SelectedIndex="0"
Height="23"
HorizontalAlignment="Left"
Margin="375,13,0,0"
VerticalAlignment="Top"
Width="151">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SchoolclassName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding SubjectName}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Well as workaround I used:
SchoolclassSubjectViewModels.Add(schoolclassSubjectVM);
SelectedSchoolclassSubjectViewModel = schoolclassSubjectVM;
and this:
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel,Mode=TwoWay}"
but I would prefer the xaml only way as it should really work.
It is because the reference inside your ItemsSource collection is not the same as the one in your SelectedItem property. I would venture to guess that you are using one object context to query your database for the list of SchoolclassSubject objects which the ItemsSource is bound to, but another context to query the actual data item to which you bind the SelectedItem. Even though the list contains a reference which represents the value held by your object, it is not really the same reference, but a separate instance of the same data.
There are ways to solve this issue, most of them involve using the SelectedValuePath and SelectedValue instead of the SelectedItem properties, but the concrete solution would be different depending on your particular ORM.

How to get number of items in ObservableCollection from XAML?

I'm displaying all of my customers which I get from a ViewModel ObservableCollectoin property within a ComboBox like this:
<ComboBox
ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
Margin="20"
HorizontalAlignment="Left"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>
Is there a way to get the number of items in the ObservableCollection without creating another ViewModel property, e.g. something like this:
PSEUDO-CODE:
<TextBlock Text="{Binding Customers.Count()}"/>
The ObservableCollection type exposes a Count Property which you can use.
I don't know if ObservableCollection raises the PropertyChanged event in order to inform the UI about updates to this property though.

Resources