Trigger Property on another WPF control event - wpf

Suppose I have a ListView and TextBox and they are located
in different containers in the same Window.
Selecting items in the listview I want the TextBox Text property to be updated
in accordance with the listview data bound selected item.
Is it possible in XAML?

Sure... Check this out
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ListBox x:Name="lb">
<TextBlock Text="Hey"/>
<TextBlock Text="There"/>
</ListBox>
<TextBlock Text="{Binding SelectedItem.Text, ElementName=lb}"/>
</StackPanel>
</Page>
You can bind to SelectedItem. In this case, I'm cheating, because I know it's a TextBlock, but in the real world you'll have to write a DataTemplate (most likely).

Related

SelectedItem of ListBox with DataTemplate

I have a Listbox with DataTemplate. The DataTemplate contains UserControl element. If I clicked on the UserControl element the Item, which contains this UserControl does not become the SelectedItem of the ListBox. If I clicked anywhere in the area of item except it's UserControl, it is selected.
How can I select the item of ListBoxby clicking on the area belongs item's UserControl?
<ListBox ItemsSource="{Binding ListOfQuestion, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1"
HorizontalContentAlignment="Stretch"
x:Name="ListOfQuestion"
SelectedItem="{Binding SelectedQuestion, Mode=TwoWay}">
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<local:usc_QuestionEdit x:Name="QuestionList"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Sounds like the user control is catching the mouse click and not the listbox. That is why it doesn't get selected. If you are using code-behind, you can use the events with the Preview prefix to catch the click higher in the control hierarchy.
To learn more about this events read about Routed Events on MSDN
Update: This post seems to have the answer you are looking for.
Hope this helps

WPF ListBox ItemsSource with DataTemplate

I have a ListBox with an ItemsSource pointing to a static variable, and a DataTemplate for the ListBox's ItemTemplate that should be displaying the Description property of the variable the ItemsSource points to
<ListBox x:Name="classificationTypeListBox"
ItemsSource="{x:Static h:AmbientHighlightingStyleRegistry.Instance}"
SelectedIndex="0" Foreground="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=(Description)}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I can put a break point on my application and view the ListBox. The ItemsSource does point to the variable I want and it looks like the ListBox is trying to display all the values because I can click and scroll down the it. However, no text is getting displayed so you can't actually tell what you're clicking. Also, while the break point is on, it says that the list box contains 0 items, maybe it should because I'm binding it, not sure. Any suggestions?
<TextBlock Text="{Binding Path=(Description)}" />
Why do you have parens in there? This syntax causes WPF to try to bind to an attached property, which is not what you want to do.

How to bind items of a TabControl to an observable collection in wpf?

What is the simplest example of binding the items of a TabControl to an ObservableCollection?
Each tab's content will have unique data, and indeed this data will have observableCollections of its own bound to the items components.
Currently I have a user control, which I would like to set as the content of each tab as soon as it is created. I also need to dynamically set the datacontext of this new user control when the tab is created. So, essentially, I would like the tabcontrol's observablecollection contain modelviews that map to the data in each tab.
On top of that, I need to do all this without violating MVVM in WPF! Any help?
Much appreciated!
Basic example :
<Window.Resources>
<DataTemplate x:Key="templateForTheContent" DataType="{x:Type vm:TheViewModelType}">
<v:YourUserControl/>
</DataTemplate>
<DataTemplate x:Key="templateForTheHeader" DataType="{x:Type vm:TheViewModelType}">
<TextBlock Text="{Binding ThePropertyToDisplayInTheHeader}"/>
</DataTemplate>
</Window.Resources>
...
<TabControl ItemsSource="{Binding YourCollection}"
ContentTemplate="{StaticResource templateForTheContent}"
ItemTemplate="{StaticResource templateForTheHeader}">
</TabControl>

Data binding custom control with parent

I have a UserControl that contains a listbox.
On the parent window, I have this UserControl and a button.
Ideally I'd like to use the ChangePropertyAction behavior on the parents button, and tie it to the UserControl's listbox count.
The idea being that if there are no entries in the listbox inside the usercontrol, the button on the parent window is hidden. The listbox is bound to an observablecollection.
Do I create a DependencyProperty to do this? I'm not sure how to bind the listbox's count to this property though.
Thanks so much for any insight into the right way to do this.
You can use a ElementName Binding to reach the ListBox state from the Button. You then want to use a BooleanToVisibilityConverter to do the magic.
Like so:
<Window x:Class="NestedTreeTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="200" Width="300">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
</Window.Resources>
<StackPanel>
<Button Visibility="{Binding ElementName=myList, Path=HasItems, Converter={StaticResource boolToVisibilityConverter}}">
Text
</Button>
<ListBox x:Name="myList">
<!--<ListBoxItem>Item A</ListBoxItem>-->
</ListBox>
</StackPanel>
</Window>
comment out, or uncomment the ListBoxItems to see it working...
I ended up using the Messenger classes from the MVVM Futures project to let the UserControl's ViewModel signal other ViewModels of the change.
This let's multiple listeners monitor for the same changes, without the need for extra Dependency Properties.

Display default selection in silverlight comboxbox

I have a silverlight combobox inside of a dataform as follows:
<dataControls:DataForm x:Name="newScheduleMasterForm" Height="350" Width="450" MinWidth="400"
VerticalAlignment="Top"
CommandButtonsVisibility="None"
Header="Add New Master Schedule"
HorizontalAlignment="Left" AutoGenerateFields="False" ContentLoaded="newScheduleMasterForm_ContentLoaded" >
<dataControls:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataControls:DataField>
<ComboBox x:Name="cbScheduleType" SelectedItem="{Binding Schedule, Mode=TwoWay}" SelectedIndex = "0"
ItemsSource="{Binding GetScheduleTypeValues, Source={StaticResource validDataSource}}"
/>
</dataControls:DataField>
</StackPanel>
</DataTemplate>
</dataControls:DataForm.EditTemplate>
</dataControls:DataForm>
The combobox cbScheduleType ItemsSource has values of "Interior" and Exterior. I am unable to display the default selected value "Interior" in the text box of the combobox. Is there a way to do it.
Thanks in advance
Mohit
The problem is that you're trying to both set SelectedIndex and bind Selected Item. I would just set Schedule to Interior in your constructor in codebehind and remove the SelectedIndex attribute.
There are some problems with data binding the SelectedItem property in the default Silverlight ComboBox control.
One way to work around this by creating a custom control that inherits from ComboBox and adds a SelectedValue dependency property.
There's a good example of this on Rockford Lhotka's blog at:
http://www.lhotka.net/weblog/SilverlightComboBoxControlAndDataBinding.aspx

Resources