I created a button column as a template column in a WPF datagrid.
When you press the button it sets the button content to the current date.
<DataGridTemplateColumn Header="Date In Source">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="BtnDateInSource" Click="DateButton" Style="{StaticResource ReceiveWorkButton}" ></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Style TargetType="{x:Type Button}" x:Key="ReceiveWorkButton" >
<Setter Property="Background" Value="Green"/>
<Setter Property="Content" Value="{Binding DateInSource}"/>
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DateInSource}" Value="{x:Null}" >
<Setter Property="Background" Value="#A3BF3B" />
<Setter Property="Content" Value="Receive Work" />
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
The current problem is that since it is a template column I can't seem to bind it to the datasource (MVVM).
So the user presses the button, and it changes the button text to the current date/time. I now want to write that back to the database.
My MVVM observable collection is InspectionStatus in class SourceInspection.
I would like to do it in XAML but maybe it has to be done with INotifyPropertyChanged?
DatagridContext:
Code Behind:
System.Windows.Data.CollectionViewSource SourceViewSource =
((System.Windows.Data.CollectionViewSource)
(this.FindResource("SourceViewSource")));
_context.SourceInspections.Load();
SourceViewSource.Source = _context.SourceInspections.Local;
I think I figured it out, used this...
updating wpf datagrid on button click
Regards
K.
Related
I have a C# wpf mvvm application and some data is coming from db. I have a combobox and what I want to achieve is: when the data from db is not in the combobox list, in the combobox I want to be writen:"Not found data". If it is possible I want to do this from the xaml.
I have tried :
<Trigger Property="SelectedItem" Value="{x:Null}">
<Setter Property="Text" Value="No Item Selected"/>
</Trigger>
but it is not working.
the easiest i can think of is to overlay a textblock
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="100">
<ComboBox x:Name="myComboBox" ItemsSource={Binding ...}"/>
</ComboBox>
<TextBlock Text="No data found" IsHitTestVisible="False" Margin="3">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, ElementName=myComboBox}" Value="{x:Null}">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
Control libraries like telerik or devexpress often contain controls with this feature.
I'm new with WPF and I have a problem. I want to enable a button only if en element of a Listbox is selected, otherwise it has to be disabled. I've tried with simple Validation Rule but it doesn't worked. Can anyone give me a hint? Ty
You don't use a ValidationRule to enable a Button but you could use a Button style with a trigger that binds to the SelectedItem property of the ListBox and sets the IsEnabled property of the Button to false if the SelectedItem property of the ListBox returns a null reference, e.g.:
<ListBox x:Name="lb">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
</ListBox>
<Button Content="Button">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, ElementName=lb}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Ok, this works well ty. But I've to enable the button when 2 conditions are satisfied (a textbox not empty and an item of the listbox selected). How can i do this?
You could add another trigger:
<ListBox x:Name="lb">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
</ListBox>
<TextBox x:Name="txt" />
<Button Content="Button">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem, ElementName=lb}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Text.Length, ElementName=txt}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
How to add style to datagridtemplatecolumn (WPF,XAML)
I have a datagrid where I am using datagridtemplate column as inside it I need a textblock and a button.
The textblock gets its data from an another view which is working fine. But the textblock is unable to have any scroll/textwrapping as I am unable to add any style to the textblock.
As a result when this textblock is getting multiline data only one line is visible.
I am looking for a functionality similar to datagridtextcolumn.elementstyle.Please advise.
I use a DataGridTemplateColumn to place an image of a trash can on every row of the grid. You could put a Button with an image as its content, instead of an Image in this example. When the user clicks on the image it will delete the row from the observable collection.
You will need to add
xmlns:i="clr-amespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform" in order to use the interaction. But you can set the styles of the textblock, the cell and the image/button as in the Xaml below.
Hope this helps.
<DataGridTemplateColumn MinWidth="30" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGridCell Style="{StaticResource YOURCellStyle}" >
<TextBlock Style="{StaticResource YOURTextBlockStyle}">
<Image Width="12"
Margin="0,0,0,0"
HorizontalAlignment="Center"
Source="pack://application:,,,/Resources/Images/DialogIcons/rubbish-bin.PNG"
Stretch="Uniform"
Style="{StaticResource YOURImageStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding
Path=DataContext.YOURDeleteComand, ElementName=YOURDataGrid}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</TextBlock>
</DataGridCell>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Style x:Key="YourImageOrButtonStyle" TargetType="ImageOrButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="ToolTip" Value="Delete"/>
</Style>
<Style x:Key="YOURCellStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="YOURTextBlockStyle" TargetType="TextBlock">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
I have a usercontrol (customized ComboBox) which I am including in another usercontrol. I want to set a style on that Customized ComboBox UserControl based on the data in the main usercontrol in which I am including my ComboBox Usercontrol.
Here is my Code
<MultiSelectComboBox:MultiUnitSelectControl x:Name="MultiUnitCombo" Grid.Row="0" Width="90" Grid.Column="0" ItemsSource="{Binding LstUnit}" Margin="0,10,0,2" FontWeight="Bold" HorizontalAlignment="Left">
<MultiSelectComboBox:MultiUnitSelectControl.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items.Count,ElementName=UnitCombo}" Value="1">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</MultiSelectComboBox:MultiUnitSelectControl.Style>
</MultiSelectComboBox:MultiUnitSelectControl>
This is my customized usercontrol. Now what I want is I want to Collapse it when the Items Count is equal to 1. UnitCombo is a combobox in my main UserControl based on its itemsCount I want to set the Visibility on the Included UserControl.
however when I apply this Style to the ComboBox in the Main UserControl It works
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=Items.Count, ElementName=UnitCombo}"
Value="1">
<Setter Property="SelectedIndex" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
But applying the same style on the Included UserControl doesnt show up anything.It kind of Hangs the whole view and shows nothing as if the controls are Hidden or something.
How can I do this?
I hope you are filling your UnitCombo by setting its ItemsSource to an ObservableCollection, then you will need to bind to ItemsSource.Count instead of Items.Count.
<DataTrigger Binding="{Binding Path=ItemsSource.Count,ElementName=UnitCombo}" Value="1">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
It will work.
I'm trying to bind some data to a TextBlock. The TextBlock is to display a slider bar value, and when the slider bar value has changed I want the TextBlock text to change colour to red.
My XAML is like this:
<Grid Height="227">
<TextBlock Margin="114,60,112,150" Name="textBlock1" Text="{Binding Path=DispVal}" Width="42" Grid.Column="1" HorizontalAlignment="Center" TextAlignment="Center" FontWeight="Bold">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChanged}" Value="true">
<Setter Property="TextBlock.Foreground" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsChanged}" Value="false">
<Setter Property="TextBlock.Foreground" Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
I have bound the TextBlock using DataContext:
texBlock1.DataContext = m_slider;
I update the m_slider object when my slider bar update handler fires.
However, I don't get any text or colour changes.
you could replace textblock with textbox and make it similar in look and feel by applying the following properties
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
Then you can use the TextChanged event in order to update its style any time text is changed by your binding