Converting Data Trigger XAML to C# - c#

<Image Width="16">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="False">
<Setter Property="Source" Value="Resources/image1.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="True">
<Setter Property="Source" Value="Resources/image2.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Referring to the XAML code above, I'm confused in how to convert it to XAML, for example the Data Trigger part, anyone has any idea?
Thanks.

After little refactor, I think u need this:
Style style = new Style(typeof(Image));
// Style Setter to handle 'false' case
style.Setters.Add(new Setter(Image.SourceProperty, new BitmapImage(new Uri("Resources/image2.png", UriKind.Relative))));
// DataTrigger to handle 'true' case
DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding("Value");
dataTrigger.Value = true;
dataTrigger.Setters.Add(new Setter(Image.SourceProperty, new BitmapImage(new Uri("Resources/image1.png", UriKind.Relative))));
style.Triggers.Add(dataTrigger);
this.image.Style = style;

Related

Editable DatagridComboBoxColumn in WPF using C#

I'm using WPF DataGrid control with dynamic columns binding at run time.(DataGrid columns are dynamic)
Sample code is as below
.xaml is having below code
<Style TargetType="ComboBox" x:Key="ComboBoxEditingStyle">
<Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
<Setter Property="IsDropDownOpen" Value="False" />
<Setter Property="IsEditable" Value="True" />
<Setter Property="SelectedValue" Value="Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
.xaml.cs file is having below code,
Binding theBinding = new Binding();
theBinding.Mode = BindingMode.TwoWay;
theBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
theBinding.ValidatesOnDataErrors = true;
DataGridComboBoxColumn colSuggestionList = new DataGridComboBoxColumn();
// theCollection is Collection<string>
colSuggestionList.ItemsSource = theCollection;
colSuggestionList.SelectedValueBinding = theBinding;
colSuggestionList.Visibility = Visibility.Visible;
colSuggestionList.EditingElementStyle = dgMainTemplate.FindResource("ComboBoxEditingStyle") as Style;
// dgMainTemplate is wpf DataGrid
dgMainTemplate.Columns.Add(colSuggestionList);
Column added properly, but I want to make this column as editable. User should be able to select either existing item from available list or enter a new value which is not exists in available list.
Here EditingElementStyle will add editable combobox but items are not showing in combobox until user selects any item.
Finally I got solution to this problem, Modified code is as below,
<Style x:Key="TextBlockComboBoxStyle" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Label Content="{TemplateBinding Text}" Style="{StaticResource {x:Type Label}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ComboBox" x:Key="ComboBoxEditingStyle">
<Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
<Setter Property="IsDropDownOpen" Value="False" />
<Setter Property="IsEditable" Value="True" />
</Style>
.cs code is,
Binding theBinding = new Binding();
theBinding.Mode = BindingMode.TwoWay;
theBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
theBinding.ValidatesOnDataErrors = true;
DataGridComboBoxColumn colSuggestionList = new DataGridComboBoxColumn();
// theCollection is Collection<string>
colSuggestionList.ItemsSource = theCollection;
colSuggestionList.SelectedValueBinding = theBinding;
colSuggestionList.Visibility = Visibility.Visible;
colSuggestionList.EditingElementStyle = dgMainTemplate.FindResource("ComboBoxEditingStyle") as Style;
colSuggestionList.EditingElementStyle = dgMainTemplate.FindResource("ComboBoxEditingStyle") as Style;
colSuggestionList.ElementStyle = dgMainTemplate.FindResource("TextBlockComboBoxStyle") as Style;
// dgMainTemplate is wpf DataGrid
dgMainTemplate.Columns.Add(colSuggestionList);

single cell background color in WPF Datagrid not all row color

I have write this code to change the color of a cell in WPF mydatagrid, but as result i have all the row colored and i don't know why.
Style style = this.FindResource("backColor") as Style;
DataGridClientFile.CellStyle = style;
<Style x:Key="backColor" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Colore}" Value="Red">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Colore}" Value="Green">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Colore}" Value="Yellow">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Colore}" Value="Cyan">
<Setter Property="Background" Value="Cyan"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Colore}" Value="White">
<Setter Property="Background" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
Here the solution :
Style style = this.FindResource("backColor") as Style;
Style styleNX = this.FindResource("backColorNX") as Style;
if (DataGridClientFile.Columns.Count >= 14)
{
DataGridColumn col = DataGridClientFile.Columns[14];
col.CellStyle = style;
DataGridColumn colNX = DataGridClientFile.Columns[15];
colNX.CellStyle = styleNX;
}

How to disable contextmenu item?

In WPF I have this code for disabling a menuitem on a certain condition:
private void gridListPlayers_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
Player player = (Player)gridListPlayers.SelectedItem;
if(player.Owner.GUID == Guid.Empty.ToString())
{
propMenuItem.IsEnabled = false;
}
else
{
propMenuItem.IsEnabled = true;
}
}
I'm trying to achieve the same result of that function via XAML. Is there a simple way to do that?
You can set the visibility on or off according to a property in your selected Item. So create read only property ToggleMenuVisible, which returns (Owner.GUID != Guid.Empty.ToString()) and then in your xaml do something like:
<MenuItem x:Name="MyToggleMenu" Header="My Toggle Menu" >
<MenuItem.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedItem.ToggleMenuVisible}" Value="False">
<Setter Property="UIElement.Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedItem.ToggleMenuVisible}" Value="True">
<Setter Property="UIElement.Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
Obviously you will need to include SelectedItem in your view's model.

Dynamically Create TextBlock and TextBox in XAML WPF

My Question Is I have a grid where number of TextBlock and TextBox will vary as per the ComboBox SelectedItem Change
So, what I want is that I have written a function for get the details of the TextBlock and TextBox.
C# Function
public void GetAdditionalAttributes()
{
using (Entities _entities = new Entities())
{
var attributeAll = (from c in _entities.AdditionalAttributeValues
where c.DeviceID == 35
select new AttributesClass { AttributeValue = c.AdditionalAttributeValue1, AttributeName = c.AdditionalAttribute.Name }).ToList();
DeviceAttributes = new ObservableCollection<AttributesClass>(attributeAll);
}
}
Now In the XAML I was trying:
<Style x:Key="AdditionalAttributeDisplay"
TargetType="Grid"
x:Name="AdditionalAttributeDisplay"
>
<Style.Resources>
<Style TargetType="ItemsControl">
<Setter Property="ItemsSource"
Value="{Binding DeviceAttributes}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedValue, ElementName=DeviceTypeComboBox}"
Value="1">
<Setter Property="ItemsSource"
Value="{Binding DeviceAttributes}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
But I don't know how to create a TextBlock or TextBox with ItemSource binding.
You can change the ItemTemplate of your ItemsControl.
<Style TargetType="ItemsControl">
<Setter Property="ItemTemplate">
<Setter.Value>
<ItemContainerTemplate>
<Grid>
<TextBox Width="100" Height="20" Text={Binding AttributeName}/>
</Grid>
</ItemContainerTemplate>
</Setter.Value>
</Setter>
</Style>
In above snippet replace PropertyName with DeviceAttributes member you want

WPF DataTrigger Binding not working

I have a search box which i am trying to to check if it is empty by using the "hasdata" and if empty return false else return true, but the DataTrigger Binding is not working. can someone point me in the right direction on what i am doing wrong.
code:
public bool hasdata
{
get { if (searchBox.Text.Count() == 0) return false; else return true; }
}
xaml:
<telerik:RadWatermarkTextBox x:Name="searchBox"/>
<Image Source="SomeImage.png" >
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=hasdata}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=hasdata}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
The UI has currently no way of being notified when hasdata is changed. You need to either implement the INotifyPropertyChanged interface or make hasdata a DependencyProperty.

Categories