-
Jul 27th, 2013, 04:51 PM
#1
[RESOLVED] Build toolbar buttons at runtime with tooltips
I was using the "commented" out code below to create BUTTONS on a TOOLBAR at runtime - worked nicely.
Code:
//List<String> list = new List<string>();
//list.Clear();
//for (int i = 0; i < x.Suggestions2.Count; i++)
//{
// list.Add(x.Suggestions2[i]);
//}
//ToolList.ItemsSource = list;
So I am now trying to build these buttons with tooltips as well - but when I run the code below it shows no buttons at all.
Code:
ToolBar tb = new ToolBar();
tb.Items.Clear();
for (int i = 0; i < x.Suggestions2.Count; i++)
{
Button ib = new Button();
ib.Content = x.Suggestions2[i];
ib.ToolTip = "tool tip " + x.Suggestions2[i];
tb.Items.Add(ib);
}
ToolList.ItemsSource = tb.ItemsSource;
//ToolList = tb; <------- I tried this first - it also did not work
Last edited by szlamany; Jul 28th, 2013 at 06:19 AM.
-
Jul 27th, 2013, 04:54 PM
#2
Re: Build toolbar buttons at runtime with tooltips
nvm - I had to do this
Code:
List<Button> list = new List<Button>();
list.Clear();
for (int i = 0; i < x.Suggestions2.Count; i++)
{
Button ib = new Button();
ib.Content = x.Suggestions2[i];
ib.ToolTip = "tool tip " + x.Suggestions2[i];
list.Add(ib);
}
ToolList.ItemsSource = list;
The BINDING in WPF is really, really cool
-
Jul 28th, 2013, 06:21 AM
#3
Re: Build toolbar buttons at runtime with tooltips
Argh - this is no longer resolved
The code I used originally nicely took from a XAML item template
Code:
<ToolBar Grid.Row="1" Grid.ColumnSpan="2" Band="1" BandIndex="1" x:Name="ToolList">
<ToolBar.ItemTemplate>
<DataTemplate>
<Grid>
<Button Margin="2" Content="{Binding}" BorderBrush="{x:Null}" Click="Button_Click">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFFCF9FC" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
</Grid>
</DataTemplate>
</ToolBar.ItemTemplate>
</ToolBar>
So the buttons had some styling. Creating the BUTTONS with the new code doesn't have the template styling.
How would I create buttons with tooltips programmatically??
Or how would I get some styling to copy from that template item while also creating these buttons?
-
Jul 29th, 2013, 01:06 AM
#4
Re: Build toolbar buttons at runtime with tooltips
I don't have VS here so perhaps you can try out if this will work?
If you already have the template perhaps you can also try this out.
Last edited by dee-u; Jul 29th, 2013 at 01:27 AM.
-
Jul 29th, 2013, 09:11 AM
#5
Re: Build toolbar buttons at runtime with tooltips
Thanks - both of those links have good info. I'm off for a few days of vacation (that's rare for me!)...
The ParseXaml - have you used that a lot? Is it slow when building "lots" of controls at runtime?
ParseXaml reminds me of how jQuery allows you to easily build new DOM/HTML stuff at runtime in a browser - very cool!
-
Jul 31st, 2013, 05:21 PM
#6
Re: Build toolbar buttons at runtime with tooltips
Ok - this became the XAML resource definition. Although I have to say that there seems to be very many ways to define resources...
Code:
<Window.Resources>
<LinearGradientBrush x:Key="ButtonBrush" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFFCF9FC" Offset="1"/>
</LinearGradientBrush>
<Style x:Key="ButtonStyle">
<Setter Property="Control.Margin" Value="2"></Setter>
<Setter Property="Control.BorderBrush" Value="{x:Null}"></Setter>
<EventSetter Event="Button.Click" Handler="Button_Click" />
</Style>
</Window.Resources>
and in code I used it like this
Code:
List<Button> list = new List<Button>();
list.Clear();
for (int i = 0; i < x.Suggestions2.Count; i++)
{
Button ib = new Button();
ib.Style = (Style)FindResource("ButtonStyle");
ib.Background = (Brush)FindResource("ButtonBrush");
.
.
.
Seems to be working as expected now - thanks again (apparently I cannot rep you till I spread some around)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|