Does anyone know if there is an option to hide a GridViewColumn somehow like this:
<ListView.View>
<GridView>
<GridViewColumn Header="Test" IsVisible="{Binding Path=ColumnIsVisible}" />
</GridView>
<ListView.View>
Thanks a lot!
Edit: For clarity
Unfortunately, there is no "IsVisible" Property. I'm looking for a way to create that.
Edit: The solution based on the feedback looks like:
<GridViewColumn DisplayMemberBinding="{Binding Path=OptionColumn1Text}"
Width="{Binding Path=SelectedEntitiy.OptionColumn1Width}">
<GridViewColumnHeader Content="{Binding Path=SelectedEntitiy.OptionColumn1Header}" IsEnabled="{Binding Path=SelectedEntitiy.OptionColumn1Width, Converter={StaticResource widthToBool}}" />
</GridViewColumn>
public class WidthToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Thanks to all!
Cheers
Edit: Reflecting the modified question.
What about creating a 0 width column? Write a Boolean to Width IValueConverter, that takes a ColumnIsVisible as the ConverterParmeter?
public class BooleanToWidthConverter : IValueConverter {
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture){
return ((bool) parameter)? value : 0;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture){
throw new NotImplementedException();
}
}
Something like:
<ListView .. >
<ListView.Resources>
<BooleanToWidthConverter x:Key="boolToWidth" />
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn
Header="Test"
Width=
"{Binding Path=ColumnWidth,
Converter={StaticResource boolToWidth},
ConverterParameter=ColumnIsVisible}" />
</GridView>
<ListView.View>
Here is another solution based on setting the column's width to zero. I have modified it a little. It now works like this:
Bind the header's visibility to a boolean property of the ViewModel, using a bool-to-visibility converter
Use an attached property on the header to set the column's width to zero
Here is the code.
XAML:
<GridViewColumn
HeaderTemplate="..."
HeaderContainerStyle="...">
<GridViewColumnHeader
Content="Header text"
Visibility="{Binding AppliesToColumnVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
behaviors:GridViewBehaviors.CollapseableColumn="True" />
BooleanToVisibilityConverter:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool param = bool.Parse(value.ToString());
if (param == true)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Attached behavior GridViewBehaviors.CollapseableColumn:
public static readonly DependencyProperty CollapseableColumnProperty =
DependencyProperty.RegisterAttached("CollapseableColumn", typeof(bool), typeof(GridViewBehaviors),
new UIPropertyMetadata(false, OnCollapseableColumnChanged));
public static bool GetCollapseableColumn(DependencyObject d)
{
return (bool)d.GetValue(CollapseableColumnProperty);
}
public static void SetCollapseableColumn(DependencyObject d, bool value)
{
d.SetValue(CollapseableColumnProperty, value);
}
private static void OnCollapseableColumnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
GridViewColumnHeader header = sender as GridViewColumnHeader;
if (header == null)
return;
header.IsVisibleChanged += new DependencyPropertyChangedEventHandler(AdjustWidth);
}
static void AdjustWidth(object sender, DependencyPropertyChangedEventArgs e)
{
GridViewColumnHeader header = sender as GridViewColumnHeader;
if (header == null)
return;
if (header.Visibility == Visibility.Collapsed)
header.Column.Width = 0;
else
header.Column.Width = double.NaN; // "Auto"
}
One simpler approach, that still uses the concept of setting the columns width to zero but does not have the side effects of using a IValueConverter (the user can still drag the column wider) is to create a new getter/setter that returns a width based on your ColumnIsVisible variable and then bind to that:
public double ColumnWidth
{
get
{
if (this.ColumnIsVisible)
{
return 100;
}
else
{
return 0;
}
}
set
{
OnPropertyChanged("ColumnWidth");
}
}
Make your bindings TwoWay and if the user attempts to drag the column wider OnPropertyChanged will be called and reset the width to 0. You might have to use a binding proxy though for your binding. Also add a call to OnPropertyChanged("ColumnWidth") when ever ColumnIsVisible is updated :)
I've set the column the width="0".
Now the column looks like its not visible. But i do not know if it will affect anything else.
It might be a dummy solution but for now it works.
Use if Thumb.DragDelta may solve the problem
I use it in listview as
<ListView x:Name="MyListView"IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Items}", Mode=Default, Source={StaticResource DataProvider}}"
Thumb.DragDelta="Thumb_DragDelta">
public Window1()
{
InitializeComponent();
MyListView.AddHandler(Thumb.DragDeltaEvent, new DragDeltaEventHandler(Thumb_DragDelta), true );
void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Thumb senderAsThumb = e.OriginalSource as Thumb;
GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader;
if (header.Column.ActualWidth < MIN_WIDTH)
{
header.Column.Width = MIN_WIDTH;
}
if (header.Column.ActualWidth > MAX_WIDTH)
{
header.Column.Width = MAX_WIDTH;
}
}
}
Related
I have a user control which is an ellipse that acts like a "led". I want to bind its "Fill" to a boolean property (State).
I used for that a boolean to Color converter.
here is the user control I did:
<UserControl x:Class="Sol.Components.Led"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:conv="clr-namespace:Sol.Converters"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Ellipse Fill="{Binding Converter={StaticResource BoolToColor}}" StrokeThickness="3" Stroke="Gray"/>
</Grid>
</UserControl>
also the converter is not recognised in the user control! I did it like this
public class BoolToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return Colors.Green; // to replace with onColor
else
return Colors.Red; // to replace with offColor
}
return Colors.LightGray;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Color)
{
if ((Color)value == Colors.Green) // to compare with onColor
{
return true;
}
}
return false;
}
}
I used a window to include 4 user contols:
<Window x:Class="Sol.Menu.Leds"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:led="clr-namespace:Sol.Components"
xmlns:conv="clr-namespace:Sol.Converters"
Title="Leds" Height="300" Width="300">
<Window.Resources>
<conv:BoolToColor x:Key="BoolToColor" />
</Window.Resources>
<Grid>
...
<led:Led Grid.Column="0" Grid.Row="0" x:Name="led1" State="False"/>
<led:Led Grid.Column="0" Grid.Row="1" x:Name="led2" State="False"/>
<led:Led Grid.Column="1" Grid.Row="0" x:Name="led3" State="False"/>
<led:Led Grid.Column="1" Grid.Row="1" x:Name="led4" State="False"/>
</Grid>
</Window>
and the used control class :
public partial class Led : UserControl
{
private bool state;
public bool State
{
get { return state; }
set { state = value; }
}
private Color onColor;
public Color OnColor
{
get { return onColor; }
set { onColor = value; }
}
private Color offColor;
public Color OffColor
{
get { return offColor; }
set { offColor = value; }
}
public Led()
{
InitializeComponent();
}
}
this is works without binding and the window shows 4 ellipses, but I am unable to change the color dynamically (from the code bedhind).
any help to fix the binding?
Try to bind to the State property of the UserControl:
<Ellipse Fill="{Binding Path=State,
RelativeSource={RelativeSource AncestorType=UserControl},
Converter={StaticResource BoolToColor}}" StrokeThickness="3" Stroke="Gray"/>
You should also return a Brush instead of a Color from your converter:
public class BoolToColor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
return Brushes.Green; // to replace with onColor
else
return Brushes.Red; // to replace with offColor
}
return Brushes.LightGray;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is Brush)
{
if ((Brush)value == Brushes.Green) // to compare with onColor
{
return true;
}
}
return false;
}
}
You need to implement PropertyChanged so the UI knows a property has been changed.
Read here how it should be done: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification
Fill, like all other UI "color" properties, is actually a Brush value, not a Color
Change your converter to return Brushes.Red / Brushes.Green.
I have a problem for WPF assoccolation
GridViewColumn Values to Combobox Items Values
I'm using enum objects for Fill to combobox.
Can't binding GridItemColumn(durum) to ComboboxItems
ENUM
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum eViziteDurumlari
{
[Description("Onaysız")]
Onaysiz = 0,
[Description("Onaylı")]
Onayli = 1,
[Description("Hepsi")]
Hepsi = 99
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum eViziteDurum
{
[Description("Onaysız")]
Onaysiz = 0,
[Description("Onaylı")]
Onayli = 1,
}
ENUM CLASSES
public class EnumDescriptionTypeConverter : EnumConverter
{
public EnumDescriptionTypeConverter(Type type)
: base(type)
{
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString();
}
}
return string.Empty;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
DATA TABLE
private DataTable DataTableOlustur()
{
Islemler = null;
using (Islemler = new DataTable())
{
Islemler.Columns.Add("firmaadi", typeof(string));
Islemler.Columns.Add("firmakodu", typeof(string));
Islemler.Columns.Add("tckimliknr", typeof(string));
Islemler.Columns.Add("adisoyadi", typeof(string));
Islemler.Columns.Add("bas_istirahat", typeof(DateTime));
Islemler.Columns.Add("bit_istirahat", typeof(DateTime));
Islemler.Columns.Add("durum", typeof(int));
return Islemler;
}
return null;
}
XAML
<ListView x:Name="lstItems">
<ListView.View>
<GridView x:Name="gridView" ScrollViewer.CanContentScroll="True" TextSearch.Text="True">
<GridViewColumn Width="120" Header="Firma Kodu" DisplayMemberBinding="{Binding firmakodu}"/>
<GridViewColumn Width="220" Header="Firma Adı" DisplayMemberBinding="{Binding firmaadi}" />
<GridViewColumn Width="120" Header="TC Kimlik No" DisplayMemberBinding="{Binding tckimliknr}"/>
<GridViewColumn Width="auto" Header="Adı Soyadı" DisplayMemberBinding="{Binding adisoyadi}" />
<GridViewColumn Width="120" Header="İstirahat Baş. Tarihi" DisplayMemberBinding="{Binding bas_istirahat,StringFormat={}{0:dd/MM/yyyy}}" />
<GridViewColumn Width="120" Header="İstirahat Bit. Tarihi" DisplayMemberBinding="{Binding bit_istirahat,StringFormat={}{0:dd/MM/yyyy}}" />
<GridViewColumn Header="Durum">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" MinWidth="150"
ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:eViziteDurum}}}"
SelectedValue="{Binding Path=durum}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I can't see DataTable Values to Combobox
I'm sorry for bad english
The ComboBox.ItemsSource is filled with enum values and it will not auto-convert the SelectedValue between int and the enum type, so a converter is needed there.
Code:
public class EnumIntegerConverter : IValueConverter
{
// probably add some code to ensure the enum type is actually set
// or move it to the converter parameter in order to use the same converter instance with different types
public Type EnumType { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// probably add some sanety checks on the involved types and values
return Enum.ToObject(EnumType, value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// probably add some sanety checks on the involved types and values
return System.Convert.ToInt32(value);
}
}
XAML resource
<local:EnumIntegerConverter x:Key="enumConverter" EnumType="{x:Type local:eViziteDurum}"/>
XAML usage
<ComboBox ... SelectedValue="{Binding Path=durum,Converter={StaticResource enumConverter}}"/>
I do have a ValueConverter that works fine on elements e.g. like a STACKPANEL. The Method User.OwnsRight(Int32) returns true or false.
[ValueConversion(typeof(object), typeof(System.Windows.Visibility))]
public class ConverterUserRightVisibility : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bxSuite.Base.Objects.User locUser = (bxSuite.Base.Objects.User)value;
if (parameter == null) return System.Windows.Visibility.Visible;
if (locUser == null) return System.Windows.Visibility.Visible;
if (locUser.OwnsRight(System.Convert.ToInt32(parameter)))
{
return System.Windows.Visibility.Visible;
}
else
{
return System.Windows.Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
System.Windows.Visibility locVisibility = (System.Windows.Visibility)value;
if (locVisibility == System.Windows.Visibility.Hidden || locVisibility == System.Windows.Visibility.Collapsed)
{
return false;
}
else
{
return true;
}
}
}
So this XAML works great:
<StackPanel Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"></StackPanel>
But if i do bind the Visibility of one of my UserControls to this Converter, the Converter doesn't even get fired. The UserControl itself works fine.
<my:MenuButtonLarge Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"/>
Any ideas why? Any help appreciated! Thanks a lot.
EDIT: i've tried another Converter simply setting text on a Label control. Works fine, but if i use it on a Textproperty (dependency property) of a usercontrol the Converter with the ConverterParameter doesn't get fired? What am I missing?
I'm trying to implement the following "start screen" interface for my Windows Store App.
I've figured a Gridview would be the component to use.
How do i display different type of items in a GridView?
Is this a good approach:
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<ContentControl Content="{Binding Converter={StaticResource local:ContentTypeToControlConverter}}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
And Class
public class ContentTypeToControlConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null)
{
if (value is MenuItem)
{
return new MenuItemControl();
}
else if (value is RecentViewItem)
{
return new RecentItemControl();
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
If you are targeting Windows 8.1 or higher - you could use the Hub control. That way you can avoid having to specify groups of items for your GridView, but implementing a DataTemplateSelector and setting it as a ItemTemplateSelector property of the GridView is the way to have items based on different templates.
May someone tell me whats wrong with this sourcecode?
when i click the button its not updating ist value?
At first binding the converter makes his job.
the sourcecode is pretty big so i will post only some snippets.
XAML:
Instances is type of ObservableCollection
<ListBox Name="Instances">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding Path=Instance.Name}" Content="{Binding Path=Instance.Active, Converter={StaticResource BTSC}}" Click="ChangeAccess"/>
<TextBlock Text="{Binding Path=Instance.Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Converter:
public class BoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((Boolean)value) == true)
return "No";
else
return "Yes";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Event:
private void ChangeAccess(object sender, RoutedEventArgs e)
{
for...
if ((sender as Button).Tag.ToString() == (DP.Instances[i].Instance as CInstance).Name)
{
SkipIfAndElse...
DP.Instances[i].Instance.Active = true;
}
}
CInstance:
class CInstance : INotifyPropertyChanged
{
private Boolean active;
public Boolean Active
{
get { return active; }
set
{
active = value;
NotifyPropertyChanged("Access");
}
}
}
All other values of the CInstance class are updating as expected.
In your CInstance class
NotifyPropertyChanged("Access");
should be
NotifyPropertyChanged("Active");
I would suggest you start using some kind of INPC framework. I personally like Simon Cropp's Fody.
Fody adds the appropriate OnNotifyPropertyChanged as a post compilation step, which means you don't get the Runtime hit that you get with Expression based solutions.
At the end of the day, string based OnNotifyPropertyChanged are all pretty fragile.