Bind visibility of text box to view model property in a content template





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am just trying to set a text box to collapsed in my GridViewColumnHeader.ContentTemplate. I believe everything is being done correctly but for some reason the text box won't collapse when the boolean property is set.



Information




  • I have a View with a View Model that has a FilterRows boolean property.

  • I am styling my grid view to allow users to set the FilterRows property and have filters for each header appear.

  • I have tried using a BooleanToVisibilityConverter and a BooleanToCollapsedConverter.

  • I have verified that the bool is being set in the view model when the user selects a check box.


BooleanToCollapsedConverter



This converter is in the same view as my grid view table. I know the converter works because I have used it on other UI elements in the same view.



   <Grid.Resources>
<ResourceDictionary>
<local:BooleanToCollapsedConverter x:Key="BooleanToCollapsedConverter"/>
</ResourceDictionary>
</Grid.Resources>


This is the class for the booleanToCollapsedConverter



public class BooleanToCollapsedConverter : BaseValueConverter<BooleanToCollapsedConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}

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


The View



Here is a the code for a part of grid view column where I am trying to use the converter within the style. The GridViewColumnHeader visibility is being set properly and collapsing as needed. The inner portion with the data template is not working though.



<GridViewColumn DisplayMemberBinding = "{Binding Description}" 
Width="auto">
<GridViewColumnHeader Content = "Description"
Visibility="{Binding HeaderList[1].IsChecked, Converter={StaticResource BooleanToCollapsedConverter}}"
local:GridViewBehaviors.CollapseableColumn="True">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Height = "25"
FontSize="{StaticResource FontSizeSmall}"
Visibility="{Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter}}"/>
<TextBlock Text = "Description" ></ TextBlock >
</ StackPanel >
</ DataTemplate >
</ GridViewColumnHeader.ContentTemplate >
</ GridViewColumnHeader >
</ GridViewColumn >


The View Model



This is my boolean property in the view model.



/// <summary>
/// True if the user wants to apply filters to the rows.
/// Once set the UI will display all filters
/// </summary>
public bool FilterRows
{
get => _filterRows;
set => Set(ref _filterRows, value);
}


enter image description here



Final Points



So overall I am just literally trying to collapse that text box when I check the Filter Rows: check box. Not sure if it has something to do with the style or I am doing something improperly? Any help would be greatly appreciated!



Thanks










share|improve this question























  • the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

    – Bizhan
    Jan 3 at 17:00




















0















I am just trying to set a text box to collapsed in my GridViewColumnHeader.ContentTemplate. I believe everything is being done correctly but for some reason the text box won't collapse when the boolean property is set.



Information




  • I have a View with a View Model that has a FilterRows boolean property.

  • I am styling my grid view to allow users to set the FilterRows property and have filters for each header appear.

  • I have tried using a BooleanToVisibilityConverter and a BooleanToCollapsedConverter.

  • I have verified that the bool is being set in the view model when the user selects a check box.


BooleanToCollapsedConverter



This converter is in the same view as my grid view table. I know the converter works because I have used it on other UI elements in the same view.



   <Grid.Resources>
<ResourceDictionary>
<local:BooleanToCollapsedConverter x:Key="BooleanToCollapsedConverter"/>
</ResourceDictionary>
</Grid.Resources>


This is the class for the booleanToCollapsedConverter



public class BooleanToCollapsedConverter : BaseValueConverter<BooleanToCollapsedConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}

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


The View



Here is a the code for a part of grid view column where I am trying to use the converter within the style. The GridViewColumnHeader visibility is being set properly and collapsing as needed. The inner portion with the data template is not working though.



<GridViewColumn DisplayMemberBinding = "{Binding Description}" 
Width="auto">
<GridViewColumnHeader Content = "Description"
Visibility="{Binding HeaderList[1].IsChecked, Converter={StaticResource BooleanToCollapsedConverter}}"
local:GridViewBehaviors.CollapseableColumn="True">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Height = "25"
FontSize="{StaticResource FontSizeSmall}"
Visibility="{Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter}}"/>
<TextBlock Text = "Description" ></ TextBlock >
</ StackPanel >
</ DataTemplate >
</ GridViewColumnHeader.ContentTemplate >
</ GridViewColumnHeader >
</ GridViewColumn >


The View Model



This is my boolean property in the view model.



/// <summary>
/// True if the user wants to apply filters to the rows.
/// Once set the UI will display all filters
/// </summary>
public bool FilterRows
{
get => _filterRows;
set => Set(ref _filterRows, value);
}


enter image description here



Final Points



So overall I am just literally trying to collapse that text box when I check the Filter Rows: check box. Not sure if it has something to do with the style or I am doing something improperly? Any help would be greatly appreciated!



Thanks










share|improve this question























  • the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

    – Bizhan
    Jan 3 at 17:00
















0












0








0








I am just trying to set a text box to collapsed in my GridViewColumnHeader.ContentTemplate. I believe everything is being done correctly but for some reason the text box won't collapse when the boolean property is set.



Information




  • I have a View with a View Model that has a FilterRows boolean property.

  • I am styling my grid view to allow users to set the FilterRows property and have filters for each header appear.

  • I have tried using a BooleanToVisibilityConverter and a BooleanToCollapsedConverter.

  • I have verified that the bool is being set in the view model when the user selects a check box.


BooleanToCollapsedConverter



This converter is in the same view as my grid view table. I know the converter works because I have used it on other UI elements in the same view.



   <Grid.Resources>
<ResourceDictionary>
<local:BooleanToCollapsedConverter x:Key="BooleanToCollapsedConverter"/>
</ResourceDictionary>
</Grid.Resources>


This is the class for the booleanToCollapsedConverter



public class BooleanToCollapsedConverter : BaseValueConverter<BooleanToCollapsedConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}

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


The View



Here is a the code for a part of grid view column where I am trying to use the converter within the style. The GridViewColumnHeader visibility is being set properly and collapsing as needed. The inner portion with the data template is not working though.



<GridViewColumn DisplayMemberBinding = "{Binding Description}" 
Width="auto">
<GridViewColumnHeader Content = "Description"
Visibility="{Binding HeaderList[1].IsChecked, Converter={StaticResource BooleanToCollapsedConverter}}"
local:GridViewBehaviors.CollapseableColumn="True">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Height = "25"
FontSize="{StaticResource FontSizeSmall}"
Visibility="{Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter}}"/>
<TextBlock Text = "Description" ></ TextBlock >
</ StackPanel >
</ DataTemplate >
</ GridViewColumnHeader.ContentTemplate >
</ GridViewColumnHeader >
</ GridViewColumn >


The View Model



This is my boolean property in the view model.



/// <summary>
/// True if the user wants to apply filters to the rows.
/// Once set the UI will display all filters
/// </summary>
public bool FilterRows
{
get => _filterRows;
set => Set(ref _filterRows, value);
}


enter image description here



Final Points



So overall I am just literally trying to collapse that text box when I check the Filter Rows: check box. Not sure if it has something to do with the style or I am doing something improperly? Any help would be greatly appreciated!



Thanks










share|improve this question














I am just trying to set a text box to collapsed in my GridViewColumnHeader.ContentTemplate. I believe everything is being done correctly but for some reason the text box won't collapse when the boolean property is set.



Information




  • I have a View with a View Model that has a FilterRows boolean property.

  • I am styling my grid view to allow users to set the FilterRows property and have filters for each header appear.

  • I have tried using a BooleanToVisibilityConverter and a BooleanToCollapsedConverter.

  • I have verified that the bool is being set in the view model when the user selects a check box.


BooleanToCollapsedConverter



This converter is in the same view as my grid view table. I know the converter works because I have used it on other UI elements in the same view.



   <Grid.Resources>
<ResourceDictionary>
<local:BooleanToCollapsedConverter x:Key="BooleanToCollapsedConverter"/>
</ResourceDictionary>
</Grid.Resources>


This is the class for the booleanToCollapsedConverter



public class BooleanToCollapsedConverter : BaseValueConverter<BooleanToCollapsedConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}

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


The View



Here is a the code for a part of grid view column where I am trying to use the converter within the style. The GridViewColumnHeader visibility is being set properly and collapsing as needed. The inner portion with the data template is not working though.



<GridViewColumn DisplayMemberBinding = "{Binding Description}" 
Width="auto">
<GridViewColumnHeader Content = "Description"
Visibility="{Binding HeaderList[1].IsChecked, Converter={StaticResource BooleanToCollapsedConverter}}"
local:GridViewBehaviors.CollapseableColumn="True">
<GridViewColumnHeader.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Height = "25"
FontSize="{StaticResource FontSizeSmall}"
Visibility="{Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter}}"/>
<TextBlock Text = "Description" ></ TextBlock >
</ StackPanel >
</ DataTemplate >
</ GridViewColumnHeader.ContentTemplate >
</ GridViewColumnHeader >
</ GridViewColumn >


The View Model



This is my boolean property in the view model.



/// <summary>
/// True if the user wants to apply filters to the rows.
/// Once set the UI will display all filters
/// </summary>
public bool FilterRows
{
get => _filterRows;
set => Set(ref _filterRows, value);
}


enter image description here



Final Points



So overall I am just literally trying to collapse that text box when I check the Filter Rows: check box. Not sure if it has something to do with the style or I am doing something improperly? Any help would be greatly appreciated!



Thanks







c# wpf xaml data-binding converters






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 16:21









SelthienSelthien

1338




1338













  • the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

    – Bizhan
    Jan 3 at 17:00





















  • the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

    – Bizhan
    Jan 3 at 17:00



















the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

– Bizhan
Jan 3 at 17:00







the property FilterRows does not belong to each item of the GridView. it belongs to another object. so you need to specify what is the object that has FilterRows and then find a way to point the binding path towards that. e.g. using x:static, RelativeSource, implement ParentViewModel, etc...

– Bizhan
Jan 3 at 17:00














2 Answers
2






active

oldest

votes


















2














try to set data source for textbox binding. How do I use WPF bindings with RelativeSource?



smth like this. typeOfAncestor is UserControl or a Window



{Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter},
RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}





share|improve this answer


























  • I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

    – Selthien
    Jan 3 at 17:29



















0














If the view model where the FilterRows property is defined is the DataContext of the parent DataGrid, this should work:



<TextBox Height="25" FontSize="{StaticResource FontSizeSmall}" 
Visibility="{Binding DataContext.FilterRows,
RelativeSource={RelativeSource AncestorType=DataGrid},
Converter={StaticResource BooleanToCollapsedConverter}}"/>


Also note that there is a built-in converter for converting between bool and Visibility that you can use instead of creating your own one:



<BooleanToVisibilityConverter x:Key="BooleanToCollapsedConverter" />





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026103%2fbind-visibility-of-text-box-to-view-model-property-in-a-content-template%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    try to set data source for textbox binding. How do I use WPF bindings with RelativeSource?



    smth like this. typeOfAncestor is UserControl or a Window



    {Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter},
    RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}





    share|improve this answer


























    • I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

      – Selthien
      Jan 3 at 17:29
















    2














    try to set data source for textbox binding. How do I use WPF bindings with RelativeSource?



    smth like this. typeOfAncestor is UserControl or a Window



    {Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter},
    RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}





    share|improve this answer


























    • I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

      – Selthien
      Jan 3 at 17:29














    2












    2








    2







    try to set data source for textbox binding. How do I use WPF bindings with RelativeSource?



    smth like this. typeOfAncestor is UserControl or a Window



    {Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter},
    RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}





    share|improve this answer















    try to set data source for textbox binding. How do I use WPF bindings with RelativeSource?



    smth like this. typeOfAncestor is UserControl or a Window



    {Binding FilterRows, Converter={StaticResource BooleanToCollapsedConverter},
    RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 16:45

























    answered Jan 3 at 16:39









    Paweł GórszczakPaweł Górszczak

    23427




    23427













    • I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

      – Selthien
      Jan 3 at 17:29



















    • I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

      – Selthien
      Jan 3 at 17:29

















    I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

    – Selthien
    Jan 3 at 17:29





    I see what your saying. I tried using ancestor type of my page and it still did not work. The grid view is contained in a regular page. I have also tried using ancestor type of grid view and list view. (Grid view is in my list view). Still isn't working for some reason.

    – Selthien
    Jan 3 at 17:29













    0














    If the view model where the FilterRows property is defined is the DataContext of the parent DataGrid, this should work:



    <TextBox Height="25" FontSize="{StaticResource FontSizeSmall}" 
    Visibility="{Binding DataContext.FilterRows,
    RelativeSource={RelativeSource AncestorType=DataGrid},
    Converter={StaticResource BooleanToCollapsedConverter}}"/>


    Also note that there is a built-in converter for converting between bool and Visibility that you can use instead of creating your own one:



    <BooleanToVisibilityConverter x:Key="BooleanToCollapsedConverter" />





    share|improve this answer






























      0














      If the view model where the FilterRows property is defined is the DataContext of the parent DataGrid, this should work:



      <TextBox Height="25" FontSize="{StaticResource FontSizeSmall}" 
      Visibility="{Binding DataContext.FilterRows,
      RelativeSource={RelativeSource AncestorType=DataGrid},
      Converter={StaticResource BooleanToCollapsedConverter}}"/>


      Also note that there is a built-in converter for converting between bool and Visibility that you can use instead of creating your own one:



      <BooleanToVisibilityConverter x:Key="BooleanToCollapsedConverter" />





      share|improve this answer




























        0












        0








        0







        If the view model where the FilterRows property is defined is the DataContext of the parent DataGrid, this should work:



        <TextBox Height="25" FontSize="{StaticResource FontSizeSmall}" 
        Visibility="{Binding DataContext.FilterRows,
        RelativeSource={RelativeSource AncestorType=DataGrid},
        Converter={StaticResource BooleanToCollapsedConverter}}"/>


        Also note that there is a built-in converter for converting between bool and Visibility that you can use instead of creating your own one:



        <BooleanToVisibilityConverter x:Key="BooleanToCollapsedConverter" />





        share|improve this answer















        If the view model where the FilterRows property is defined is the DataContext of the parent DataGrid, this should work:



        <TextBox Height="25" FontSize="{StaticResource FontSizeSmall}" 
        Visibility="{Binding DataContext.FilterRows,
        RelativeSource={RelativeSource AncestorType=DataGrid},
        Converter={StaticResource BooleanToCollapsedConverter}}"/>


        Also note that there is a built-in converter for converting between bool and Visibility that you can use instead of creating your own one:



        <BooleanToVisibilityConverter x:Key="BooleanToCollapsedConverter" />






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 4 at 9:43

























        answered Jan 4 at 8:36









        mm8mm8

        89.3k81934




        89.3k81934






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026103%2fbind-visibility-of-text-box-to-view-model-property-in-a-content-template%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]