Setting Image properties in XAML template - c#

So I have a template for a button with an image and a textblock. The images I have are too large, and I'd like to scale them down.
<Style x:Key="ImageButton" TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Image Source="{Binding Path=(local:ButtonProperties.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />
<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></ContentPresenter>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Is the specific block of code I have. However, when I try to add Height="X" to the template, I get errors. Is there a way to specify the image size within the template itself?

Related

How do I style the ToggleButton in the Expander control WPF

I'm currently trying to style the ToggleButton in the Expander control
but I can't figure out how to change it's Background color and the Foreground color.
I've currently managed to change the Text in the border but I can seem to find how to target the ToggleButton, is it under some other name?
This is what I've got so far.
<Style TargetType="Border" x:Key="RacePitBorderStyle" >
<Setter Property="Background" Value="{StaticResource BackBrush}"/>
</Style>
<DataTemplate x:Key="titleText">
<Border Style="{StaticResource RacePitBorderStyle}" Height="24">
<TextBlock Text="{Binding}"
Margin="75,0,0,0"
VerticalAlignment="Center"
Foreground="White"
FontSize="11"
FontWeight="Medium"
Width="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type Expander}},
Path=ActualWidth}"/>
</Border>
</DataTemplate>
<Style TargetType="{x:Type Expander}">
<Setter Property="HeaderTemplate" Value="{StaticResource titleText}"/>
</Style>

Not able to display checkboxes for ListViewItems in WPF

I want to display checkboxes alongside text in ListView. I have multiple ListViews in my WPF window, hence I want to specify this behavior as a style in resources. But the checkboxes are not displayed, only the text is. So please, find below my code and suggest edits.
<Window.Resources>
<Style TargetType="ListView" x:Key="ListViewTemplate">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Content=""></CheckBox>
<Separator Width="5"></Separator>
<TextBlock Text="{Binding Text}"></TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListView Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="8" Grid.RowSpan="5" x:Name="listViewDocTypes" Style="{DynamicResource ListViewTemplate}" >
</ListView>
</Grid>
Provide a Text or Width to the Checkbox. Without setting the Text property the length will automatically be "0" if you do not specify its Width.
<CheckBox Content="xyz" Width="1234"/>
The problem was that I was binding data the wrong way.I was using the following code.
ListViewItem item = new ListViewItem();
item.Tag = docType;
item.Text = docType.Text;
listViewDocTypes.Items.Add(item);
The correct way to bind is
listViewDocTypes.ItemsSource = docTypes;

How to bind property of level2 templated children from the level0 templated parent

I have a Control template defined for my custom control say ControlA:
<Style TargetType="local:ControlA">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ControlA">
<Grid>
<FlipView VirtualizingStackPanel.VirtualizationMode="Recycling"
ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Canvas
ZIndex="1"
Visibility="Collapsed">
<Border BorderBrush="Black"
BorderThickness="1">
<TextBlock Name="CurrentTimeMarkerTB"/>
</Canvas>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipVIew>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I want to bind the test property of CurrentTimeMarkerTB to some property of ControlA. I tried
Text="{Binding SomeDateTimePropertyOfControlA,
RelativeSource={RelativeSource TemplatedParent},
Converter={StaticResource TimelineFormatter},
ConverterParameter='0:hh:mm tt', TargetNullValue='CurrentTime'}"
But it never triggeres the converter's TimelineFormatter code.
There is no TemplatedParent in a DataTemplate, only in ControlTemplate. You need to retrieve the Parent using FindAncestor:
Text="{Binding SomeDateTimePropertyOfControlA,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ControlA}},
Converter={StaticResource TimelineFormatter},
ConverterParameter='0:hh:mm tt', TargetNullValue='CurrentTime'}"
If you can't use FindAncestor mode of RelativeSource you can use ElementName:
Text="{Binding SomeDateTimePropertyOfControlA,
ElementName=MyControlA,
Converter={StaticResource TimelineFormatter},
ConverterParameter='0:hh:mm tt', TargetNullValue='CurrentTime'}"
And then name your control:
<local:ControlA x:Name="MyControlA"/>

ListView Universal Menu

I'm building a touch screen application that needs to give the user the ability to quickly copy and paste text in a listview. I've created the menu, but now I am trying to prevent repetitive XAML. I have the following template for the Cell:
<DataTemplate x:Key="copyPaste">
<Button Click="cell_click" Tag="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
<TextBlock Text="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}}" />
</Button>
</DataTemplate>
I want to implement it similar to this:
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Tag="{Binding Serial}" DataTemplate="{DynamicResource copyPaste}"></Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
I don't necessarily want a "Grid" element, but I need a way to reference the value that is suppose to be entered on that cell.
Grid -> DataTemplate does not exist. Is there another element / tag or another way I should be trying to do this? Am I going about this the wrong way?
I've gotten the following to work in a way that I think is acceptable. I don't know if it was the best way or not.
In the DefaultResources.XAML, I have:
<ControlTemplate x:Key="copyPaste" TargetType="Button">
<TextBlock Text="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />
</ControlTemplate>
<Style x:Key="copyPasteStyle" TargetType="Button">
<Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"></Setter>
</Style>
Then in the actual list view, I have
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Tag="{Binding Serial}" Template="{DynamicResource copyPaste}" Click="cell_click" Style="{DynamicResource copyPasteStyle}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
The ContentPresenter is apparently no longer necessary to remove the button graphic since the "Template" for the button is being set.

(WPF) How to change button image background in listBox with buttons in each line ?

i have listbox, and the data template is button :
<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button x:Name="btnname" Content="{Binding name}" Click="btnname_Click">
<Button.Background>
<ImageBrush ImageSource="/myApplication;component/images/buttons/normal.png"/>
</Button.Background>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
i have two image backgroud for this listBox image (normal.png for normal mode, click.png for selected item in listBox)
The listBox view items in list of buttons, the button image background is normally normal.png
my question is how to change the button image background to click.png for selected one and the old selected button retrieve to normal.png
How to change image background for selected item in listBox with buttons in each line ?
hope this clear, please i spend about one day about this issue
can any one help
Thanks
I haven't tested it, but you need some code that looks like this:
<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button x:Name="btnname" Click="btnname_Click">
<Grid>
<Image>
<Image.Style>
<Style>
<Setter Property="Image.Source"
Value="/myApplication;component/images/buttons/normal.png" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Value="True">
<Setter Property="Image.Source"
Value="/myApplication;component/images/buttons/click.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding name}" HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The idea is to bind directly to the IsSelected property of the ListBoxItem object. This is done using a RelativeSource binding. However, I'm guessing that this code doesn't do what you want... I suggest that you might want to use a ToggleButton instead... something like this:
<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="btnname" Click="btnname_Click" IsChecked="{Binding
IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type
ListBoxItem}}}">
<Grid>
<Image>
<Image.Style>
<Style>
<Setter Property="Image.Source"
Value="/myApplication;component/images/buttons/normal.png" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Value="True">
<Setter Property="Image.Source"
Value="/myApplication;component/images/buttons/click.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding name}" HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</ToggleButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
if you are using MVVM then make a property in viewModel and bind this to ImageBrush as ImageSource,
when you are selecting value in ListView then get selected record and change image path of this property.
Try using Togglebutton instead of button.Use a trigger to check when the IsChecked property of the toggleButton changes. And based on that change your image.

Categories