I want to add button in combobox, which contains ItemTemplate. First, that I tried was this:
<ComboBox Name="oilWells_comboBox"
Style="{StaticResource MMComboBox}"
MaxWidth="100"
ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
Margin="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
<CheckBox.Content>
<TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</CheckBox.Content>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
</ComboBox>
But I've got an exception, which said what i cant add items to control, which has ItemTemplate. The second one was this:
<ComboBox Name="oilWells_comboBox"
Style="{StaticResource MMComboBox}"
MaxWidth="100"
ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
Margin="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
<CheckBox.Content>
<TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
<Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
</CheckBox.Content>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
But in this case button adds after each checkbox. Have you any ideas, how to do this only once? Thanks you in advance)
Right Click ComboBox in designer surface / or in Document Outline at the left side outside designer surface > Edit Template > Edit copy.
This will generate some Styles under Window.Resources, find ItemsPresenter and wrap it with StackPanel and Button as shown below :
<StackPanel Grid.ColumnSpan="2">
<Button Content="Additional"/>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</StackPanel>
Try this in windows loaded event and it should work.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Button b = new Button();
b.Content = "My Button";
b.Click += new RoutedEventHandler(MyBtn_Click);
oilWells_comboBox.Items.Add(b);
}
Related
I have spent wayyyy too much time on this, so if someone can help point me in the right direction, I would be very appreciative.
I have an Expander with a ListBox in it. I have bound the ListBox to a List ie FilteredProjects. I would like the Header of the expander to reflect the number of items in the displayed list. I did implement INotifyPropertyChanged for ProjectsCount, and the ItemsSource for the ListBox updates just fine. Thanks in advance for your time.
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Projects" />
<Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="#FF545F42" Margin="1,0,0,0" >
<Label Content="{Binding Path=ProjectsCount, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource filter}}" Padding="0" FontSize="8" FontStyle="Italic"></Label>
</Border>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<ListBox x:Name="ProjectsFilterListView" Opacity="1" ItemsSource="{Binding FilteredProjects}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding ItemName}" Margin="10,10,0,10"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
You could just get the Item Count directly from the ListBox
<Label Content="{Binding Path=Items.Count, ElementName=ProjectsFilterListView}" />
Here is an example of it working, I replaced Label for TextBlock so I could use StringFormat to add "Items:" text before the number as I am not sure what your Converter is doing
<StackPanel Orientation="Horizontal">
<TextBlock Text="Projects" />
<Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="Red" Margin="1,0,0,0" >
<TextBlock Text="{Binding Path=Items.Count, ElementName=ProjectsFilterListView, StringFormat={} Items: {0}}" Padding="0" FontSize="10" FontStyle="Italic" Foreground="Black"/>
</Border>
</StackPanel>
Result:
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
my listbox:
<ListBox x:Name="listBox" Grid.Row="2" FontSize="26" SelectionChanged="listBox_SelectionChanged" ItemsSource="{Binding SelectedSubGenre.PhotoCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="BurlyWood" BorderThickness="1,1,1,1" Margin="0,0,12,24">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Image Source="{Binding Path}" Height="200" HorizontalAlignment="Left" Width="200" Margin="12,12,12,12"/>
<Button Foreground="yellow" Click="Button_Click" Height="150" Width="150" Content="add"/>
</StackPanel>
<TextBlock Text="{Binding Name}" Margin="12,0,0,0"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked I want to do something with the neighboring image of the button, how can I specify this particular image in my code? How can I get the neighboring image based on different buttons clicks
In the button click callback, do the following:
var btn = sender as Button;
var dc = btn.DataContext as YourDataObject;
dc.Path = "/path/to/new/image.jpg";
You might have to use a value converter to change the path from a string into a BitmapImage.
I have a Toolbar whose ItemSource is a collection of toolbarItems which contain the bitmap text and other info for the button and the xaml includes a DataTemplate to bind the data to the button.
Our app now needs to become 508 compliant and when I run the Accessible Event Watcher it is listing all the toolbar button names as "Unknown".
Can someone tell me how to provide a meaningful name to the buttons?
Here's the portion of xaml applying to this issue:
<ToolBar.ItemTemplate>
<DataTemplate DataType="{x:Type src:toolBarItem}">
<DataTemplate.Resources>
<src:toolBarItemConverter x:Key="buttonConverter" />
<src:booleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
<src:toolBarButtonFormatConverter x:Key="toolBarFormatDisplayConverter" />
<src:stringToVisibilityConverter x:Key="stringToVisibilityDisplayConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Border Style="{StaticResource SeparatorStyle}" Visibility="{Binding menuSeparator, Converter={StaticResource boolToVisibilityConverter}}"/>
<Button x:Name="listButton" Height="{Binding menuHeight, Mode=OneWay}" Width="{Binding menuWidth}" VerticalAlignment="Top" HorizontalAlignment="Center" Visibility="{Binding isActiveButton, Converter={StaticResource boolToVisibilityConverter}}" Tag="{Binding}"
ToolTip="{Binding menuTooltip}" IsEnabled="{Binding isEnabled}" >
<UniformGrid VerticalAlignment="Center" HorizontalAlignment="Center" Rows="{Binding menuText,Converter={StaticResource toolBarFormatDisplayConverter}}" >
<!-- button image -->
<Image Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding menuImage, Converter={StaticResource buttonConverter}}"/>
<!-- button name -->
<Viewbox StretchDirection="DownOnly" HorizontalAlignment="Center" VerticalAlignment="Bottom" Visibility="{Binding menuText, Converter={StaticResource stringToVisibilityDisplayConverter}}" >
<TextBlock x:Name="buttonName" FontFamily="Segoe UI" Width="{Binding menuWidth}" FontSize="12" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding menuText}" Foreground="Black" />
</Viewbox>
</UniformGrid>
<!-- </StackPanel> -->
</Button>
</StackPanel>
</DataTemplate>
</ToolBar.ItemTemplate>
Thanks,
Ron
OK we figured this out. Need to simply bind your names to the AutomationProperties.Name
<Button x:Name="listButton" AutomationProperties.Name="{Binding menuText}"
Height="{Binding menuHeight, Mode=OneWay}" Width="{Binding menuWidth}"
VerticalAlignment="Top" HorizontalAlignment="Center"
Visibility="{Binding isActiveButton,
Converter={StaticResource boolToVisibilityConverter}}"
Tag="{Binding}" ToolTip="{Binding menuTooltip}" IsEnabled="{Binding isEnabled}" >
How to get the content of the hyperlink button clicked in the customized list. The code of XAML is posted below.
<ListBox Height="513" HorizontalAlignment="Left" Margin="9,88,0,0" Name="listBox1" VerticalAlignment="Top" Width="436">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
<StackPanel Width="370">
<HyperlinkButton Content="{Binding usrname}" Click="eventhandler" Foreground="#FFC8AB14" FontSize="24" />
<TextBlock Text="{Binding msg}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Thanks alot in advance
Your event handler will have a source argument which is a reference to the HyperlinkButton. You can use it as follows:
public void eventhandler(object source, EventArgs e)
{
HyperlinkButton button = source as HyperlinkButton;
var foo = button.Content;
}