I have a textbox with an Binding to an ObservableCollection with a triple tuple and want to change the Binding depending on two radiobuttons:
XAML:
<ItemsControl ItemsSource="{Binding DataInformation, Mode=OneWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="xTextbox" Grid.Row="0"
IsReadOnly="True"
Text="{Binding Path=Item2 , Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl >
<RadioButton Grid.Row="1"
x:Name="PatoRadioButton"
Width="150"
Content="Type Pato"
Checked="PatoRadioButtonChecked" />
<RadioButton Grid.Row="2"
x:Name="FifoRadioButton"
Width="150"
Content="Type Fifo"
Checked="FifoRadioButtonChecked" />
VIEWMODEL:
private ObservableCollection<Tuple<int, string, string>> dataInformation;
public ObservableCollection<Tuple<int, string, string>> DataInformation
{
get
{
return DataInformation;
}
set
{
dataInformation = value;
NotifyPropertyChanged("DataInformation");
}
}
QUESTION:
How can I change the Text="{Binding Path=Item2 , Mode=OneWay}" of the TextBox depending on the checked RadioButton?
PatoRadioButton is checked then: Text="{Binding Path=Item2 , Mode=OneWay}"
FifoRadioButton is checked then: Text="{Binding Path=Item3 , Mode=OneWay}"
You could add 2 Triggers to the TextBox of your DataTemplate:
<DataTemplate>
<TextBox x:Name="xTextbox" Grid.Row="0" IsReadOnly="True">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked,
ElementName=PatoRadioButton}" Value="True">
<DataTrigger.Setters>
<Setter Property="Text" Value="{Binding Path=Item2,
Mode=OneWay" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked,
ElementName=FifoRadioButton}" Value="True">
<DataTrigger.Setters>
<Setter Property="Text" Value="{Binding Path=Item3,
Mode=OneWay}" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
The triggers will set the Binding of the TextBlock when the IsChecked property is changed.
Related
I using xceed components in our project.
Now what I need to do is readOnly property for timePicker.
Currently I got this:
<DockPanel LastChildFill="True" Width="Auto" MinWidth="140" Height="25" Margin="2">
<TextBlock Text="Od " TextAlignment="Right" VerticalAlignment="Center" FontSize="15"/>
<xceed:DateTimePicker Format="Custom"
FormatString="{Binding Path=CustomDateTimeFormat, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
FontSize="15" Value="{Binding Path=FromDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</xceed:DateTimePicker>
</DockPanel>
But I want timePicker from this DateTimePicker readOnly = true or false, depend on conditions.
Im using MVVM pattern.
Set a DataTrigger that changes the IsReadOnly property based on some property in the VM:
<xctk:DateTimePicker
FontSize="15" Value="{Binding Path=FromDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<xctk:DateTimePicker.Style>
<Style TargetType="xctk:DateTimePicker">
<Setter Property="IsReadOnly" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="True">
<Setter Property="IsReadOnly" Value="SomeValue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</xctk:DateTimePicker.Style>
</xctk:DateTimePicker>
I have a DataGrid with a DataGridTemplateColumn.CellTemplate defined like this:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox ToolTip="Select all items"
IsChecked="{Binding IsSelected}" Name="chkSelectAll" Checked="AllItem_Checked"
Unchecked="UnCheckAll_UnChecked" IsHitTestVisible="{Binding Path=IsSelected}"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" Checked="Item_Checked" Unchecked="Item_UnChecked"
IsChecked="{Binding IsSelected}" IsHitTestVisible="{Binding Path=IsSelected}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The grid has a binding with a list of objects.
I want, that for each object in ItemsSource, that the checkbox is readonly if the IsSelected attribute is true.
I don't know how to code this; I tried Binding IsHitTestVisible property to IsSelected ItemsSource Objects attribute but it's not working.
I googled about and found some topics about Multidatatriggers but can't understand how they work.
You could use a Style with a DataTrigger that disables the CheckBox if IsSelected returns true:
<CheckBox HorizontalAlignment="Center" Checked="Item_Checked" Unchecked="Item_UnChecked" IsChecked="{Binding IsSelected}">
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
I would like to change the behavior of a button in an DataGridTemplateColumn by CellTemplateSelector if the row is newly added or in edit mode.
The button should normaly labeld "DELETE" but when in edit mode it should be labeld "INSERT" and call an other method.
Here what I have found on other examples. But I don't figure out, how to access the DataGrid(Row) in the DataGridButtonSelector.
in my XAML:
<Window.Resources>
<local:DataGridButtonSelector x:Key="DataGridButtonSelector" />
</Window.Resources>
...
<DataGrid x:Name="dgMassnahmen" AutoGenerateColumns="False" Margin="10,10,10.667,46.667" ColumnWidth="SizeToCells" RowHeight="62" RowEditEnding="dgMassnahmen_RowEditEnding" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserSortColumns="False" AddingNewItem="dgMassnahmen_AddingNewItem">
<DataGrid.Resources>
<DataTemplate x:Key="insertTemplate">
<Button x:Name="btnDelete" Content="Einfügen" Click="Button_Click_2" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext, Converter={StaticResource GridViewButtonVisibilityConverter}}"></Button>
</DataTemplate>
<DataTemplate x:Key="deleteTemplate">
<Button x:Name="btnDelete" Content="Entfernen" Click="btnDelete_Click" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=DataContext, Converter={StaticResource GridViewButtonVisibilityConverter}}">
</Button>
</DataTemplate>
</DataGrid.Resources>
...
<DataGridTemplateColumn Width="1*" Header="" CellTemplateSelector="{StaticResource DataGridButtonSelector}">
...
and my DataGridButtonSelector.cs:
class DataGridButtonSelector : DataTemplateSelector
{
public DataTemplate insertTemplate { get; set; }
public DataTemplate deleteTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
// HERE I STUCK!
if (//THE ROW IS IN EDIT MODE (AND SELECTED))
return
element.FindResource("insertTemplate") as DataTemplate;
else
return
element.FindResource("deleteTemplate") as DataTemplate;
}
}
you can simply use a trigger and set whatever you want in it:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="agggsdf" ></TextBlock>
<Button Name="f"></Button>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsEditing}" Value="True">
<Setter TargetName="f" Property="Content" Value="remove"></Setter>
<Setter TargetName="f" Property="Style">
<Setter.Value>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="Button_Click"></EventSetter>
</Style>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsEditing}" Value="False">
<Setter TargetName="f" Property="Content" Value="insert"></Setter>
<Setter TargetName="f" Property="Style">
<Setter.Value>
<Style TargetType="Button">
<EventSetter Event="Click" Handler="f_Click"></EventSetter>
</Style>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I am currently displaying a hyperlink in my data grid with the following DataGridHyperLinkColumn definition:
<DataGridHyperlinkColumn Header="Item" Binding="{Binding Item, Mode=OneWay}">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="ButtonItemInfo_OnClick"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
I want to change this to conditionally display a hyperlink or a label (or textblock). So if the bound value is "SH", I want to display the label. Otherwise I want the hyperlink.
How can I accomplish this?
I would bind the Hyperlink to a command and conditionally return false in CanExecute. You can style the hyperlink for disabled state.
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Item">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Command="{Binding DataContext.Navigate, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}">
<TextBlock Text="{Binding}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And the ViewModel:
public ObservableCollection<string> Items
{
get;
set;
}
public ICommand Navigate
{
get
{
return new RelayCommand(
(param) => DoNavigate(param as string), // execute
(param) => // can execute
{
var link = param as string;
return link != "SH";
});
}
}
If you really need a textbox for some rows you can use a DataTrigger in the columns cell style.
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Item">
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<!-- Template for normal rows -->
<ControlTemplate>
<TextBlock>
<Hyperlink Command="{Binding DataContext.Navigate,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}">
<TextBlock Text="{Binding}" />
</Hyperlink>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="SH">
<Setter Property="Template">
<Setter.Value>
<!-- Template for SH rows -->
<ControlTemplate>
<TextBlock Text="{Binding}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I am having an issue with WPFToolkit DataGrid when a column is customized supplying both CellTemplate and CellEditingTemplate. If you take a look below, you will see my editing template has a single CheckBox. All is fine in a functional sense but when F2 is hit to edit the cell, one must also hit TAB in order for the CheckBox to receive focus. Ideally, one would hit F2 and SPACE to toggle the value. Currently, one must hit F2, TAB, SPACE. I have tried setting TabIndex to no avail. I am running out of ideas.
<WPFToolkit:DataGridTemplateColumn Header="Turn"
MinWidth="60">
<WPFToolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="16">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding CanTurn}" Value="True">
<Setter Property="Source" Value="/Images/16/Tick.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</WPFToolkit:DataGridTemplateColumn.CellTemplate>
<WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
</DataTemplate>
</WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFToolkit:DataGridTemplateColumn>
Try this
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox Name="checkbox" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
<DataTemplate.Triggers>
<Trigger SourceName="checkbox" Property="IsVisible" Value="True">
<Setter TargetName="checkbox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=checkbox}" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Or this...
<DataGridTemplateColumn Header="Long" IsReadOnly="False" Width="100">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" IsChecked="{Binding Path=CanTurn}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
if you want to set the focus on edit and select the text given by a Binding try this.
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Parameter0, Mode=TwoWay}" Loaded="TbLoaded" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
code behind:
private void TbLoaded(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if (tb == null) return;
tb.SelectAll();
FocusManager.SetFocusedElement(this, tb);
}