Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:46:35+00:00 2026-06-16T00:46:35+00:00

So i created a userControl with a ShowCode property to learn how to use

  • 0

So i created a userControl with a “ShowCode” property to learn how to use properties. I want this property to hide the second row in the grid.

View:

    <UserControl x:Class="Test.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    <Grid Margin="0,0,0,0" Name="outergrid" DataContext="{Binding}">
      <Grid.RowDefinitions>
        <RowDefinition Height="3*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>

    <ContentControl Name="XAMLView" Grid.Row="0"/>
    <GridSplitter ResizeDirection="Rows" 
                Grid.Row="0"
                VerticalAlignment="Bottom" 
                HorizontalAlignment="Stretch" />
    <Border Width="11" Grid.Row="1" Background="Black" />
</Grid>

Code:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        outergrid.RowDefinitions[1].SetBinding(RowDefinition.HeightProperty, new Binding() { Path = new PropertyPath("ShowCode"), Source = this, Converter = new BoolToHeightConverter(), ConverterParameter = "True" });
    }

    public bool ShowCode
    {
        get { return (bool)GetValue(ShowCodeProperty); }
        set { SetValue(ShowCodeProperty, value); }
    }

    public static readonly DependencyProperty ShowCodeProperty =
        DependencyProperty.Register("ShowCode",
        typeof(bool),
        typeof(UserControl1),
        new PropertyMetadata(true, new PropertyChangedCallback(OnShowCodeChanged)));

    static void OnShowCodeChanged(object sender, DependencyPropertyChangedEventArgs args)
    {
        UserControl1 source = (UserControl1)sender;

        //source.outergrid.RowDefinitions[1].Height = 
    }

    public class BoolToHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if ((string)parameter == "False") return "0";
            return "1*";
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            if ((int)parameter == 0) return false;
            return true;
        }
    }
}

Problem: When I use it like this:

<xamlviewer:UserControl1 ShowCode="False"/>

Convert(…) is called 2 times and both times the “parameter” is “True”, otherwise if ShowCode=”True” Convert() is called only once and “parameter” is again “True”

Why is it always true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:46:36+00:00Added an answer on June 16, 2026 at 12:46 am

    At least two things are wrong here.

    1. Your converter returns a string where it should return a GridLength.

    2. The value to be converted is passed to the value parameter of the converter, not to parameter and it is a bool.

    So you should write this:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool && (bool)value)
        {
            return new GridLength(1, GridUnitType.Star);
        }
    
        return new GridLength(0);
    }
    

    No converter parameter is needed in the binding:

    outergrid.RowDefinitions[1].SetBinding(
        RowDefinition.HeightProperty,
        new Binding()
        {
            Path = new PropertyPath("ShowCode"),
            Source = this,
            Converter = new BoolToHeightConverter()
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a dependency property like this: public partial class MyControl: UserControl { //...
I have created a UserControl in WPF. This user control consists of a border,
I have created a UserControl as follows: <UserControl x:Class=MySample.MyControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:local=using:MySample xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
This is my UserControl created in Blend: <StackPanel Orientation=Horizontal Background=#FF0084FF Height=400> <TextBlock TextWrapping=Wrap Margin=10,0,20,0
I've created some UserControl. Here is its constructor: public ZoomableChart() { InitializeComponent(); this.mainChart =
I’ve created UserControl: public partial class MyTemplate : UserControl { public MyUser User {
I created a UserControl and added a label asset. I gave this label a
I've created UserControl and I don't know how to set Name property when I
I have a created a UserControl with a combobox in it. This combobox is
İ downloaded gMap.dll and created an userControl which include Map.When i used this userControl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.