0

i have a problem binding the visibility property in code behind and really hope, you can help me.

I create a data grid column with a button. The tag property of the button is already binded to the data grid column value (text). I now want to set a visibility binding for the button, so that the button is hidden, when the text (in the Tag property) is empty. Unfortunately, nothing happened when the button tag is empty. The breakpoint in the converter never reaches.

// Create new column with a button content (this works!)
DataGridTemplateColumn dgButtonColumn = new DataGridTemplateColumn();
dgButtonColumn.CellTemplate = new DataTemplate(typeof(Button));
FrameworkElementFactory fefButton = new FrameworkElementFactory(typeof(Button));

// Set Content property (this works!)
fefButton.SetValue(Button.ContentProperty, "Text123");

// Bind Tag property (this works!)
Binding binding = new Binding(sqlColumnName) { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
fefButton.SetBinding(Button.TagProperty, binding);

// Bind Visibility property **(this doesn't work!)**
Binding bindingVisibility = new Binding("Tag") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = new Converter.TextNotEmptyToVisibilityConverter() };
bindingEnable.Source = fefButton;
fefButton.SetBinding(Button.VisibilityProperty, bindingVisibility);

For better understanding here the code of my converter class:

namespace ABC.Converter
{
    public class TextNotEmptyToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.IsNullOrEmpty(value.ToStr()) ? Visibility.Hidden : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Can you help me fixing it, so that the button will automatically disappears, when the Tag value is empty?

Thank you so much!

4
  • There is bindingVisibility = new Binding("Tag") ..., but bindingEnable.Source = fefButton;. How does that fit together? And why would use the intermediate Tag Binding, instead of directly binding the Button's Visibilty to sqlColumnName?
    – Clemens
    Commented Jun 12, 2020 at 7:49
  • @Clemens: new Binding("Tag") defines, that we want to Bind to the Tag property, isn't it? bindingEnable.Sourcedefines, that the object from the path ("Tag") to bind to. But the hint to directly bind to the sqlColumnName was great, it seems like that it works now. Thank you for the quick and good help, @Clemens. Commented Jun 12, 2020 at 8:20
  • I understand the code, but not why there is bindingVisibility and bindingEnable, i.e. apparently two different Bindings.
    – Clemens
    Commented Jun 12, 2020 at 8:25
  • Got it. Yes, this was another bug in the code :-/. Stupid. Nevertheless, thank you!! Commented Jun 12, 2020 at 8:30

0