Binding SelectedItem inside another ListBox - xaml

I'm trying to bind SelectedItem, which is inside another ListBox, however I get this binding error from output:
System.Windows.Data Error: BindingExpression path error: 'Time' property not found on '[23, System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' 'System.Collections.Generic.KeyValuePair`2[System.Int32,System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' (HashCode=1268928309). BindingExpression: Path='Time' DataItem='[23, System.Collections.Generic.List`1[MarsrutaiAPI.ArrivalTime]]' (HashCode=1268928309); target element is 'System.Windows.Controls.ListBox' (Name='innerList'); target property is 'SelectedItem' (type 'System.Object')..
The nested listbox is declared as such:
<ListBox ItemsSource="{Binding TimeTable}" Visibility="{Binding IsLoading, Converter={StaticResource BooleanToVisibilityNegativeConverter}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Key}" Style="{StaticResource NormalText}" FontSize="42" Margin="0,0,10,0" HorizontalAlignment="Center" />
<ListBox Grid.Column="1" Name="innerList" ItemsSource="{Binding Value}" SelectedItem="{Binding Time,Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBoxItem Margin="0,0,5,5"/>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="{StaticResource tekstas}" BorderThickness="1" CornerRadius="5" Margin="5" Background="{Binding IsAccessible,Converter={StaticResource IsAccessibleToColor}}">
<TextBlock Style="{StaticResource NormalText}" Text="{Binding ExpectedTime, StringFormat=\{0:mm\}}" Margin="10" HorizontalAlignment="Center"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border Grid.Row="1" Grid.ColumnSpan="2" Margin="10"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
TimeTable property:public Dictionary<int, List<ArrivalTime>> TimeTable
Time property is an object of ArrivalTime
The whole binding works perfectly, except for the SelectedItem, even though I have the required property in my VM
private ArrivalTime _time;
public ArrivalTime Time
{
get { return _time; }
set
{
_time = value;
RaisePropertyChanged("Time");
}
}
Thanks for the help :)

try this:
SelectedItem="{Binding Path=Time, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
And if RelativeSource is not possible then name your Parent container Grid like this
<Grid Name="gridFoo" Tag={Binding} />
And
<ListBox Grid.Column="1" Name="innerList" ItemsSource="{Binding Value}" SelectedItem="{Binding Tag.Time, ElementName=gridFoo}" ScrollViewer.VerticalScrollBarVisibility="Disabled">

Related

Can't get VariableSizedWrapGrid to wrap

I'm having issues getting the VariableSizedWrapGrid to wrap horizontally. All I can seem to get are my elements stacked vertically in a single column. I'm not sure what I'm missing.
Ideally I would like to have 3 columns of input fields. The number of fields changes depending on the table selected so they need to just, you know, wrap.
<ScrollViewer
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<VariableSizedWrapGrid
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Orientation="Horizontal">
<ItemsControl
ItemsSource="{x:Bind ViewModel.CurrentRow.Values}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:RowValue">
<Grid Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
Text="{x:Bind key.ColumnValidation.column_label}"
Margin="0"
Padding="0"
Grid.Column="0">
</TextBlock>
<TextBox
Grid.Column="1"
Visibility="{x:Bind vm:Converters.IsTextBoxField(key.ColumnValidation.data_type)}"
Text="{x:Bind value}">
</TextBox>
<RichTextBlock
Visibility="{x:Bind vm:Converters.IsHyperlinkField(key.ColumnValidation.data_type)}">
<Paragraph>
<Span>
<Hyperlink />
</Span>
</Paragraph>
</RichTextBlock>
<DatePicker
Visibility="{x:Bind vm:Converters.IsDateField(key.ColumnValidation.data_type)}">
</DatePicker>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</VariableSizedWrapGrid>
</ScrollViewer>
Turns out I didn't fully understand how ItemsControl works. Managed to get it working like so:
<ScrollViewer
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ItemsControl
Height="Auto"
Width="Auto"
ItemsSource="{x:Bind EditRow.Values}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid
Orientation="Horizontal"
MaximumRowsOrColumns="4"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
ItemWidth="200"
ItemHeight="75">
</VariableSizedWrapGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:RowValue">
<Grid
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Width="190"
Margin="5">
<TextBox
Header="{x:Bind key.ColumnValidation.column_label}"
Visibility="{x:Bind vm:Converters.IsTextBoxField(key.ColumnValidation.data_type)}"
Text="{x:Bind value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<TextBox
Header="{x:Bind key.ColumnValidation.column_label}"
Visibility="{x:Bind vm:Converters.IsHyperlinkField(key.ColumnValidation.data_type)}"
Text="{x:Bind value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<!-- todo: create converter for fusion date format -->
<Viewbox VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<DatePicker
Header="{x:Bind key.ColumnValidation.column_label}"
Visibility="{x:Bind vm:Converters.IsDateField(key.ColumnValidation.data_type)}">
</DatePicker>
</Viewbox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>

Drop Event is Not Firing in UWP XAML(Windows 10)

I am developing an App For WIndows 10 and I want to Implement the Drag and Drop Frature within Two List. but the Drop Event is Not Firing in Windows 10 App..
Previously it is Woring for Windows 8.1..
Following is My Code:
<ListView Grid.Row="1" x:Name="TasksList" SelectionMode="None" HorizontalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Hidden" IsItemClickEnabled="True"
VerticalAlignment="Stretch"
ItemsSource="{Binding Tasks}" ScrollViewer.VerticalScrollMode="Enabled"
CanReorderItems="True" ShowsScrollingPlaceholders="False"
DragItemsStarting="GridViewDragItemsStarting" AllowDrop="True" IsSwipeEnabled="False"
Drop="GridViewDrop" DragEnter="TasksList_DragEnter" CanDragItems="True"
ItemContainerStyle="{StaticResource ClientListViewItemStyle}" >
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,1" BorderBrush="{StaticResource MydesqBorderBrush}" Padding="10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Width="80" Height="60" Background="{Binding DueDateIndicatorColor,Converter={StaticResource HexToSolidColorBrushConverter}}" VerticalAlignment="Top" HorizontalAlignment="Center">
<Image x:Name="ImgClient" Source="{Binding Client.ClientPictureUrl,Converter={StaticResource ServerUrlConverter}}" Stretch="Fill" Visibility="{Binding Source, Converter={StaticResource NullToInvisibilityConverter}, ElementName=ImgClient}" Width="80" Height="60"/>
<Image x:Name="ImgAccount" Source="{Binding ImageUrl}" Width="35" Height="35" Visibility="{Binding Source, Converter={StaticResource NullToInvisibilityConverter}, ElementName=ImgAccount}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<Grid Grid.Column="1" Margin="10,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding TaskTitle}" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="20" Foreground="{Binding TitleColor, Converter={StaticResource HexToSolidColorBrushConverter}}"/>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Image Width="20" Height="20" VerticalAlignment="Center" Source="/Assets/Images/user_gray.png" Margin="0,0,10,0"/>
<TextBlock Text="{Binding TaskType}" FontSize="16" VerticalAlignment="Center" Foreground="{Binding SubTitleColor, Converter={StaticResource HexToSolidColorBrushConverter}}" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Image Width="20" Height="20" VerticalAlignment="Center" Source="/Assets/Images/calendar_gray.png" Margin="0,0,10,0"/>
<TextBlock Text="{Binding DueDate, ConverterParameter=\{0:dd.MM.yyyy\}, Converter={StaticResource DateToStringConverter}}" FontSize="16" VerticalAlignment="Center" Foreground="{Binding SubTitleColor, Converter={StaticResource HexToSolidColorBrushConverter}}" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="ItemClick">
<behaviors:NavigateWithEventArgsToPageAction
TargetPage="Mydesq.Client.UWP.Views.AddTaskPage"
EventArgsParameterPath="ClickedItem" />
</Core:EventTriggerBehavior>
<Core:EventTriggerBehavior EventName="Drop">
<Core:InvokeCommandAction Command="{Binding DropTaskCommand}" CommandParameter="{Binding ElementName=TasksList,Path=SelectedItem}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ListView>
Make sure you set the AcceptedOperation property in the DragEnter event of the ListView. E.g. like this:
private void TasksList_DragEnter(object sender, DragEventArgs e)
{
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy;
}
I had similar issue with Grid in MainPage
AllowDrop="True"
fixed that even without DragEnter event.
(I know you already have this property (my answer is for others...))

How to use ScrollViewer the controls on binding?

Following code I had. I am using a Listbox using binding. Below the list box I have a button for submit. Able to scroll Listbox items only unable to scroll the button. Button is available at the bottom of the screen(not List). I want this button at the bottom of the Listbox?.
<ScrollViewer VerticalScrollBarVisibility="Visible" Height="780" MaxHeight="1800" VerticalAlignment="Top">
<ScrollViewer.Content>
<Grid Grid.Row="0" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox Name="formDetails" ItemsSource="{Binding}" Grid.Row="0" Height="780" MaxHeight="1800">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="Text">
<TextBlock Name="Txt_Question"
Text="{Binding Question, Mode=OneWay}"/>
<TextBox Name="TxTAnswer"
Text="{Binding Stringval, Mode=TwoWay}"
Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource TextConverter}}"/>
<CheckBox Name="BoolVal"
IsChecked="{Binding BoolVal, Mode=TwoWay}"
Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource YesNoConverter}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" Click="Button_Click" Content="Submit"/>
</Grid>
</ScrollViewer.Content>
</ScrollViewer>
Try this for button at the bottom of the Listbox:
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Top">
<StackPanel>
<ListBox Name="formDetails" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="Text">
<TextBlock Name="Txt_Question"
Text="{Binding Question, Mode=OneWay}"/>
<TextBox Name="TxTAnswer"
Text="{Binding Stringval, Mode=TwoWay}"
Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource TextConverter}}"/>
<CheckBox Name="BoolVal"
IsChecked="{Binding BoolVal, Mode=TwoWay}"
Visibility="{Binding DataType, Mode=OneWay, Converter={StaticResource YesNoConverter}}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Click="Button_Click" Content="Submit"></Button>
</StackPanel>
</ScrollViewer>
for button at the bottom of the page
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Top">
<StackPanel>
<ListBox Name="formDetails" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="Text">
<TextBlock Name="Txt_Question" Text="{Binding Q}"></TextBlock>
<TextBox Name="TxTAnswer" Text="{Binding A}"></TextBox>
<CheckBox Name="BoolVal" IsChecked="{Binding Val}"></CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</ScrollViewer>
<Button Click="Button_Click" Content="Submit" Grid.Row="1"></Button>
</Grid>
A few things:
You have defined your button as being in Grid.Row="1" which doesn't exist. You need to add a second row otherwise it will just appear in the same row as the ListBox.
Your entire page is within a ScrollViewer, is that really what you want? If you want the Button outside of the scrollviewer then just wrap the ListBox within the ScrollViewer, but that would be kind of pointless because a ListBox has its own ScrollViewer built in.

Localization with LocalizedStrings in Xaml - Binding value to the binding string

I'm on a question-spree!
I want to know, when binding objects to an ItemsControl, how can I bind a textbox to LocalizedStrings using the current path in de ItemsSource?
Code will clarify, I hope:
<ItemsControl ItemsSource="{Binding Hours}" VerticalAlignment="Top">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=LocalizedResources.DayName, Source={StaticResource LocalizedStrings}}" Style="{StaticResource App_Content_Grid_Bold}" HorizontalAlignment="Left" />
<TextBlock Grid.Column="1" Text="{Binding Description}" Style="{StaticResource App_Content_Grid_Subtle}" FontSize="20" Foreground="Black" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The problem is, DayName does not exist in LocalizedStrings but it's value does.
How can I parse it's value into this binding context before the binding itself is applied?
Something like: {Binding Path=LocalizedResources.[DayName], Source={StaticResource LocalizedStrings}}?

2 DataTemplates within each other, Command not found

My ICommand is not firing when i touch my button...
I have a DataTemplate within a DataTemplate... does DataContext still refer to the entire page or is it that DataContext refers to the previous DataTemplate and that's why it cannot find my view model's ICommand?
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top">
<Border BorderThickness="0 0 0 2" BorderBrush="{StaticResource xLightGray}" Margin="0,0,0,10" Padding="0,0,0,10">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Background="Transparent">
<Border Background="White" BorderThickness="0" Width="40" Height="40" HorizontalAlignment="Left">
<Image Source="{Binding image.thumbnail_link}" Width="40" Height="40"></Image>
</Border>
</StackPanel>
<StackPanel Grid.Column="1" VerticalAlignment="Center" Background="Transparent">
<TextBlock Text="{Binding name}" HorizontalAlignment="Left" FontSize="30" VerticalAlignment="Center" Padding="10,0,0,0" />
</StackPanel>
</Grid>
</Border>
<phone:LongListSelector x:Name="OrganisationItemList"
Background="Transparent"
ItemsSource="{Binding spaces}"
LayoutMode="List"
VerticalContentAlignment="Stretch">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Button Background="Red" Style="{StaticResource xTransparentButton}" Command="{Binding Path=DataContext.LoadSpaceCommand, ElementName=SpaceList}" CommandParameter="{Binding}" Padding="0,0,0,5" Margin="0" Height="auto" BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" UseLayoutRounding="True" FontSize="0.01">
<StackPanel Grid.Column="1" VerticalAlignment="Center" Background="Transparent">
<TextBlock Padding="0,0,0,0" Text="{Binding name}" HorizontalAlignment="Left" FontSize="{StaticResource xFontSize}" />
</StackPanel>
</Button>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
Found the answer, you must set ElementName in the binding to the parent ListBox:
Command="{Binding Path=DataContext.LoadSpaceCommand, ElementName=OrganisationList}"