I have implemented a listbox with buttons as follows
<Button.Content>
<StackPanel Orientation="Horizontal" Width="440" Height="210">
<Image Source="{Binding Image}" Height="120" Width="120"/>
<StackPanel Orientation="Vertical" Height="200">
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="TITLE" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding Title}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="Detail" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding description}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Width="100" Text="Discount" Height="40" FontSize="22"/>
<TextBlock Width="220" Text="{Binding discount}" Height="40" FontSize="22"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Height="40" >
<TextBlock Width="100" Text="Deal ID" Height="40" FontSize="22"/>
<TextBlock Name="dealID" Width="220" Text="{Binding DID}" Height="40" FontSize="22"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now the problem that i face is when i click the button i want to get the value of dealID thats in the last text block binded to DID. For this i created the event handler for button click as follows
private void Button_Click(object sender, RoutedEventArgs e)
{
Button myButton = sender as Button;
}
Now i dont have any clue what to do next the mybutton.content method is not working for me
I suggest you to use Blend for that, you can declare click event on xaml, its more easy.
u might want to cast the mybutton.content to the template that you have written inside the content i.e. to a stackpanel..
Related
I have a long list coming from my Business logic which I need to display on the UI. As the list is long, I tried adding Scrollviewer but I am unable to scroll.
please find the XAML code below
<Grid Margin="0,32,0,0">
<TextBlock Text="{Binding IDC_WiFi, Source={StaticResource Resources}}" FontFamily="Segoe UI" FontSize="20" Foreground="#4cb5ab" HorizontalAlignment="Left" />
<Button Command="{Binding HardwareWifiAccordionCommand}" BorderThickness="0" Width="16" HorizontalAlignment="Right" Height="16" >
<Button.Background>
<ImageBrush ImageSource="{Binding AccordionImageHardwareWifi}" />
</Button.Background>
</Button>
</Grid>
<TextBlock Text="Klein's, Anil's" FontFamily="Segoe UI" FontSize="15" Foreground="#8fa3ad"/>
<StackPanel Height="200" Visibility="{Binding IsAccordionHardwareWifi, Converter={StaticResource Bool2Visible}}">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding WifiList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,32,0,0">
<Grid>
<Image Source="/Images/Assets/da_wifi1_16x16.png" Height="16" Width="16" HorizontalAlignment="Left" />
<TextBlock Margin="25,0,0,0" Text="{Binding NetworkName}" FontSize="15" Foreground="#FFFFFF" />
<TextBlock Text="" FontSize="15" Foreground="#8fa3ad" HorizontalAlignment="Right" />
</Grid>
<TextBlock Text="" FontSize="15" Foreground="#8fa3ad" HorizontalAlignment="Left" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</StackPanel>
Put it into a ScrollViewer.
<ScrollViewer>
<StackPanel >
</StackPanel>
</ScrollViewer>
As #StepUp points out you can just wrap it with a ScrollViewer but I believe this breaks virtualization. That's outside the scope of this question of course but it's something to keep in mind. If performance is likely to become an issue then I'd suggest implementing this as shown in the answer to this question.
The scrollviewer needed a Height to be set
<Grid Margin="0,32,0,0">
<TextBlock Text="{Binding IDC_WiFi, Source={StaticResource Resources}}" FontFamily="Segoe UI" FontSize="20" Foreground="#4cb5ab" HorizontalAlignment="Left" />
<Button Command="{Binding HardwareWifiAccordionCommand}" BorderThickness="0" Width="16" HorizontalAlignment="Right" Height="16" >
<Button.Background>
<ImageBrush ImageSource="{Binding AccordionImageHardwareWifi}" />
</Button.Background>
</Button>
</Grid>
<TextBlock Text="Klein's, Anil's" FontFamily="Segoe UI" FontSize="15" Foreground="#8fa3ad"/>
<StackPanel Height="200" Visibility="{Binding IsAccordionHardwareWifi, Converter={StaticResource Bool2Visible}}">
<ScrollViewer VerticalScrollBarVisibility="Auto" Height="350">
<ItemsControl ItemsSource="{Binding WifiList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,32,0,0">
<Grid>
<Image Source="/Images/Assets/da_wifi1_16x16.png" Height="16" Width="16" HorizontalAlignment="Left" />
<TextBlock Margin="25,0,0,0" Text="{Binding NetworkName}" FontSize="15" Foreground="#FFFFFF" />
<TextBlock Text="" FontSize="15" Foreground="#8fa3ad" HorizontalAlignment="Right" />
</Grid>
<TextBlock Text="" FontSize="15" Foreground="#8fa3ad" HorizontalAlignment="Left" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</StackPanel>
My listbox XAML looks like this:
<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text=" -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="400">
<TextBlock Text="{Binding prddsc}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
<StackPanel>
<TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="20"></StackPanel>
<StackPanel Width="20">
<Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
</StackPanel>
</StackPanel>
<StackPanel Width="180">
<TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
</StackPanel>
<StackPanel Width="100">
<TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Right" ></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:
TextBlock txtBlk = new TextBlock();
txtBlk = lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";
Please let me know how can I:
set the text of that textblock prsent inside a datatemplate of a my listbox ?
i want to change the text of that textblock of that single listbox item only ?
How can I do this? Please let me know.
You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.
Sometimes there is no other solution.
See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx
I have a code like that:
<ListBox Height="522" HorizontalAlignment="Left" Margin="20,162,0,0" Name="listBox1" VerticalAlignment="Top" Width="448" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<Image Source="{Binding IconSource}" Height="48" Width="48" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding Text}" FontSize="36" VerticalAlignment="Top" Margin="0,20,20,0" Width="380" Height="Auto"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In Listbox i have 2 for-loop elements - image (icon) and text.
I need to fill the background of a particular image, but I do not know how to do it. In other words:
sorry for little stupid question and thanks for answers.
If I understand your problem correctly, you just need to add a background to the StackPanel. Change the color and opacity properties until you get the effect that you want.
<ListBox Height="522" HorizontalAlignment="Left" Margin="20,162,0,0" Name="listBox1" VerticalAlignment="Top" Width="448" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132" Padding>
<StackPanel.Background>
<SolidColorBrush Color="Black" Opacity="0.4" />
</StackPanel.Background>
<Image Source="{Binding IconSource}" Height="48" Width="48" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<TextBlock Text="{Binding Text}" FontSize="36" VerticalAlignment="Top" Margin="0,20,20,0" Width="380" Height="Auto"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I am trying to add different button controls inside the listbox dynamically for each listbox item along with the data while populating it. To achieve this, under the datatemplate tag, I have added new stack panel in which I placed button controls. Now, I am trying to find the particular stackpanel in which the buttons are being placed and then make it visible true or false depending on my condition.
So, I have tried to find the desired stack panel to get the control over it through looping the listbox items. However, I am getting "Null Reference Exception" while iterating the listbox items.
The following is my xaml code and the later is my xaml.cs code:
<ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="0,0,0,4">
<StackPanel Orientation="Vertical" Width="456">
<StackPanel Orientation="Vertical">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<StackPanel x:Name="stacktest" Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="PU: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="DO: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stknotchecked" VerticalAlignment="Top" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="300" x:Name="btnaccepttrip" Content="accept trip"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnrejecttrip" Content="reject"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnshowmap" Content="show map"></Button>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stkaccepted">
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndirections" Content="directions"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnflightinfo" Content="flight info"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndetails" Content="details"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnlogtimes" Content="log times"></Button>
</StackPanel>
<StackPanel>
<Button x:Name="btnstatus" Content="set status"></Button>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void addbtncontrols()
{
foreach (TripsList lst in TripList.Items)
{
ListBoxItem item = TripList.ItemContainerGenerator.ContainerFromItem(lst) as ListBoxItem;
Button stk = FindFirstElementInVisualTree<Button>(item);
var stkitem =(Button)stk.FindName("btndirections");
stkitem.Visibility = Visibility.Collapsed;
}
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
Could someone please let me know the way to reslove my issue. Thanks in advance..
Navigating the visual tree to locate these elements and set their visibility manually is the wrong way to go about solving this problem! I see you are using databinding. Why not expose various properties of type Visibility in the model or view model you are binding to the UI, you can then bind these to the Visibility property of the various UI elements in order to show / hide them.
As an aside, learn about Styles, you XAML has numerous repeated style properties which make it lengthy and hard to follow. You should be able to define a TextBlock style that is applied to all of your TextBlocks to remove much of the repeated code in your question.
From the above description, can you provide more information about this:
Where did you invoke addbtncontrol method? If you are responding to an event, let say: Add one more item, then it should be done inside ViewModel.
"your condition": What kind of condition? Can we model it in the Listbox item ViewModel?
I have implemented something the same: Listbox with dynamic items and custom template. Also provide the ability to add new item. So, If you give me more information on what you want to accomplish or better your view model, i can help.
I know a only little about dock panel, following is the code is used:
<DockPanel LastChildFill="True" >
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" VerticalAlignment="Center" Margin="10" >
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" VerticalAlignment="Center" >
<TextBlock Height="24" Name="Welcome" Text="Welcome" FontSize="14" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" VerticalAlignment="Center" >
<TextBlock Height="24" Name="Welcomee" Text="Welcomee" FontSize="14" />
</StackPanel>
</StackPanel>
</DockPanel>
The result is like this:
WelcomeWelcomee
However, the code must result in something like this:
Welcome Welcomee
So could you please tell me where I misunderstood the concept.
Your first StackPanel should not contain the other two stack panels.
It contains the other two. DockPanel.Dock="left" only applies to the immediate children of the DockPanel.
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" VerticalAlignment="Center" Margin="10" >Top
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left" VerticalAlignment="Center" >
<TextBlock Height="24" Name="Welcome" Text="Welcome" FontSize="14" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" VerticalAlignment="Center" >
<TextBlock Height="24" Name="Welcomee" Text="Welcomee" FontSize="14" />
</StackPanel>
</DockPanel>