WPF. Button in DataGrid Visibility












0















How to change Button Visibility in DataGrid if specific text is in row cell?



<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ConfirmEvent"
Visibility="if (SensorValueText == "qwerty") Visible"
Margin="0"
Content=""
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
IsEnabled="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>









share|improve this question





























    0















    How to change Button Visibility in DataGrid if specific text is in row cell?



    <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <Button Click="ConfirmEvent"
    Visibility="if (SensorValueText == "qwerty") Visible"
    Margin="0"
    Content=""
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    IsEnabled="True"/>
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>









    share|improve this question



























      0












      0








      0








      How to change Button Visibility in DataGrid if specific text is in row cell?



      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <Button Click="ConfirmEvent"
      Visibility="if (SensorValueText == "qwerty") Visible"
      Margin="0"
      Content=""
      HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      IsEnabled="True"/>
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>









      share|improve this question
















      How to change Button Visibility in DataGrid if specific text is in row cell?



      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <Button Click="ConfirmEvent"
      Visibility="if (SensorValueText == "qwerty") Visible"
      Margin="0"
      Content=""
      HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      IsEnabled="True"/>
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>






      c# wpf button






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 8:06









      Foo

      1




      1










      asked Nov 21 '18 at 7:59









      Дмитрий СуворовДмитрий Суворов

      236




      236
























          3 Answers
          3






          active

          oldest

          votes


















          0














          A datatrigger would look something like:





              <DataTemplate>
          <Button Click="ConfirmEvent">
          <Button.Style>
          <Style TargetType="Button">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
          <DataTrigger Binding="{Binding SensorValue}" Value="qwerty">
          <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          </Style.Triggers>
          </Style>
          </Button.Style>
          </Button>
          </DataTemplate>




          I see you are using events there.
          The next problem you are likely to find is working out which row the user clicked the button on.
          Bear in mind pretty much all wpf teams use a pattern called MVVM.
          With this, you would bind a command.
          That would either go in a viewmodel used per row - and you'd copy data to and back from each viewmodel.
          Or
          You could put the command in a parent viewmodel which is used as the datacontext of the window and pass the row as a parameter to that.
          A sample that illustrates these techniques is:
          https://gallery.technet.microsoft.com/WPF-Command-and-Row-in-84635e1a






          share|improve this answer
























          • Yes, Thank you. I already did the same

            – Дмитрий Суворов
            Nov 21 '18 at 10:05



















          2














          Why dont you create a new Converter class?



          class TextToVisibilityConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          if (value.ToString() == "someValue")
          {
          return Visibility.Visible;
          }
          return Visibility.Collapsed;
          }

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


          And then in the xaml, you can call the converter:



          <local:TextToVisibilityConverter x:Key="TextConverter"/>


          Then, you can call it inside the button:



          <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
          <Button Visibility="{Binding SomeText, Converter={StaticResource
          TextToVisibilityConverter}}....>
          </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>


          Feel free to ask if there are any follow-ups.






          share|improve this answer
























          • I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

            – Дмитрий Суворов
            Nov 21 '18 at 9:34











          • If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

            – Andy
            Nov 21 '18 at 9:39











          • @Andy I could not apply datatrigger. How to I can use it?

            – Дмитрий Суворов
            Nov 21 '18 at 9:44











          • Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

            – Petar Stojanovski
            Nov 21 '18 at 10:35





















          -2














          it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function






          share|improve this answer
























          • No, it's not. This should be done e.g. via a Style-trigger or a bound property.

            – Lennart
            Nov 21 '18 at 8:13











          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%2f53407526%2fwpf-button-in-datagrid-visibility%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          A datatrigger would look something like:





              <DataTemplate>
          <Button Click="ConfirmEvent">
          <Button.Style>
          <Style TargetType="Button">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
          <DataTrigger Binding="{Binding SensorValue}" Value="qwerty">
          <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          </Style.Triggers>
          </Style>
          </Button.Style>
          </Button>
          </DataTemplate>




          I see you are using events there.
          The next problem you are likely to find is working out which row the user clicked the button on.
          Bear in mind pretty much all wpf teams use a pattern called MVVM.
          With this, you would bind a command.
          That would either go in a viewmodel used per row - and you'd copy data to and back from each viewmodel.
          Or
          You could put the command in a parent viewmodel which is used as the datacontext of the window and pass the row as a parameter to that.
          A sample that illustrates these techniques is:
          https://gallery.technet.microsoft.com/WPF-Command-and-Row-in-84635e1a






          share|improve this answer
























          • Yes, Thank you. I already did the same

            – Дмитрий Суворов
            Nov 21 '18 at 10:05
















          0














          A datatrigger would look something like:





              <DataTemplate>
          <Button Click="ConfirmEvent">
          <Button.Style>
          <Style TargetType="Button">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
          <DataTrigger Binding="{Binding SensorValue}" Value="qwerty">
          <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          </Style.Triggers>
          </Style>
          </Button.Style>
          </Button>
          </DataTemplate>




          I see you are using events there.
          The next problem you are likely to find is working out which row the user clicked the button on.
          Bear in mind pretty much all wpf teams use a pattern called MVVM.
          With this, you would bind a command.
          That would either go in a viewmodel used per row - and you'd copy data to and back from each viewmodel.
          Or
          You could put the command in a parent viewmodel which is used as the datacontext of the window and pass the row as a parameter to that.
          A sample that illustrates these techniques is:
          https://gallery.technet.microsoft.com/WPF-Command-and-Row-in-84635e1a






          share|improve this answer
























          • Yes, Thank you. I already did the same

            – Дмитрий Суворов
            Nov 21 '18 at 10:05














          0












          0








          0







          A datatrigger would look something like:





              <DataTemplate>
          <Button Click="ConfirmEvent">
          <Button.Style>
          <Style TargetType="Button">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
          <DataTrigger Binding="{Binding SensorValue}" Value="qwerty">
          <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          </Style.Triggers>
          </Style>
          </Button.Style>
          </Button>
          </DataTemplate>




          I see you are using events there.
          The next problem you are likely to find is working out which row the user clicked the button on.
          Bear in mind pretty much all wpf teams use a pattern called MVVM.
          With this, you would bind a command.
          That would either go in a viewmodel used per row - and you'd copy data to and back from each viewmodel.
          Or
          You could put the command in a parent viewmodel which is used as the datacontext of the window and pass the row as a parameter to that.
          A sample that illustrates these techniques is:
          https://gallery.technet.microsoft.com/WPF-Command-and-Row-in-84635e1a






          share|improve this answer













          A datatrigger would look something like:





              <DataTemplate>
          <Button Click="ConfirmEvent">
          <Button.Style>
          <Style TargetType="Button">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
          <DataTrigger Binding="{Binding SensorValue}" Value="qwerty">
          <Setter Property="Visibility" Value="Visible"/>
          </DataTrigger>
          </Style.Triggers>
          </Style>
          </Button.Style>
          </Button>
          </DataTemplate>




          I see you are using events there.
          The next problem you are likely to find is working out which row the user clicked the button on.
          Bear in mind pretty much all wpf teams use a pattern called MVVM.
          With this, you would bind a command.
          That would either go in a viewmodel used per row - and you'd copy data to and back from each viewmodel.
          Or
          You could put the command in a parent viewmodel which is used as the datacontext of the window and pass the row as a parameter to that.
          A sample that illustrates these techniques is:
          https://gallery.technet.microsoft.com/WPF-Command-and-Row-in-84635e1a







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 10:03









          AndyAndy

          3,0431106




          3,0431106













          • Yes, Thank you. I already did the same

            – Дмитрий Суворов
            Nov 21 '18 at 10:05



















          • Yes, Thank you. I already did the same

            – Дмитрий Суворов
            Nov 21 '18 at 10:05

















          Yes, Thank you. I already did the same

          – Дмитрий Суворов
          Nov 21 '18 at 10:05





          Yes, Thank you. I already did the same

          – Дмитрий Суворов
          Nov 21 '18 at 10:05













          2














          Why dont you create a new Converter class?



          class TextToVisibilityConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          if (value.ToString() == "someValue")
          {
          return Visibility.Visible;
          }
          return Visibility.Collapsed;
          }

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


          And then in the xaml, you can call the converter:



          <local:TextToVisibilityConverter x:Key="TextConverter"/>


          Then, you can call it inside the button:



          <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
          <Button Visibility="{Binding SomeText, Converter={StaticResource
          TextToVisibilityConverter}}....>
          </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>


          Feel free to ask if there are any follow-ups.






          share|improve this answer
























          • I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

            – Дмитрий Суворов
            Nov 21 '18 at 9:34











          • If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

            – Andy
            Nov 21 '18 at 9:39











          • @Andy I could not apply datatrigger. How to I can use it?

            – Дмитрий Суворов
            Nov 21 '18 at 9:44











          • Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

            – Petar Stojanovski
            Nov 21 '18 at 10:35


















          2














          Why dont you create a new Converter class?



          class TextToVisibilityConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          if (value.ToString() == "someValue")
          {
          return Visibility.Visible;
          }
          return Visibility.Collapsed;
          }

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


          And then in the xaml, you can call the converter:



          <local:TextToVisibilityConverter x:Key="TextConverter"/>


          Then, you can call it inside the button:



          <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
          <Button Visibility="{Binding SomeText, Converter={StaticResource
          TextToVisibilityConverter}}....>
          </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>


          Feel free to ask if there are any follow-ups.






          share|improve this answer
























          • I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

            – Дмитрий Суворов
            Nov 21 '18 at 9:34











          • If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

            – Andy
            Nov 21 '18 at 9:39











          • @Andy I could not apply datatrigger. How to I can use it?

            – Дмитрий Суворов
            Nov 21 '18 at 9:44











          • Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

            – Petar Stojanovski
            Nov 21 '18 at 10:35
















          2












          2








          2







          Why dont you create a new Converter class?



          class TextToVisibilityConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          if (value.ToString() == "someValue")
          {
          return Visibility.Visible;
          }
          return Visibility.Collapsed;
          }

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


          And then in the xaml, you can call the converter:



          <local:TextToVisibilityConverter x:Key="TextConverter"/>


          Then, you can call it inside the button:



          <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
          <Button Visibility="{Binding SomeText, Converter={StaticResource
          TextToVisibilityConverter}}....>
          </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>


          Feel free to ask if there are any follow-ups.






          share|improve this answer













          Why dont you create a new Converter class?



          class TextToVisibilityConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          if (value.ToString() == "someValue")
          {
          return Visibility.Visible;
          }
          return Visibility.Collapsed;
          }

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


          And then in the xaml, you can call the converter:



          <local:TextToVisibilityConverter x:Key="TextConverter"/>


          Then, you can call it inside the button:



          <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
          <Button Visibility="{Binding SomeText, Converter={StaticResource
          TextToVisibilityConverter}}....>
          </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>


          Feel free to ask if there are any follow-ups.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 8:53









          Petar StojanovskiPetar Stojanovski

          686




          686













          • I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

            – Дмитрий Суворов
            Nov 21 '18 at 9:34











          • If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

            – Andy
            Nov 21 '18 at 9:39











          • @Andy I could not apply datatrigger. How to I can use it?

            – Дмитрий Суворов
            Nov 21 '18 at 9:44











          • Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

            – Petar Stojanovski
            Nov 21 '18 at 10:35





















          • I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

            – Дмитрий Суворов
            Nov 21 '18 at 9:34











          • If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

            – Andy
            Nov 21 '18 at 9:39











          • @Andy I could not apply datatrigger. How to I can use it?

            – Дмитрий Суворов
            Nov 21 '18 at 9:44











          • Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

            – Petar Stojanovski
            Nov 21 '18 at 10:35



















          I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

          – Дмитрий Суворов
          Nov 21 '18 at 9:34





          I'm not very strong yet about C#. I tryed to use your code. I changed Converter={StaticResource TextToVisibilityConverter}} to Converter={StaticResource TextConverter}} It works! Thank you!

          – Дмитрий Суворов
          Nov 21 '18 at 9:34













          If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

          – Andy
          Nov 21 '18 at 9:39





          If you're comparing the entire value for a string then a datatrigger would work, It's only really if you need a partial match then you'd need a converter.

          – Andy
          Nov 21 '18 at 9:39













          @Andy I could not apply datatrigger. How to I can use it?

          – Дмитрий Суворов
          Nov 21 '18 at 9:44





          @Andy I could not apply datatrigger. How to I can use it?

          – Дмитрий Суворов
          Nov 21 '18 at 9:44













          Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

          – Petar Stojanovski
          Nov 21 '18 at 10:35







          Yes, you are right, I made a typo. It should be "..StaticResource TextConverter..". If you accept the answer, please do so. stackoverflow.com/help/accepted-answer

          – Petar Stojanovski
          Nov 21 '18 at 10:35













          -2














          it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function






          share|improve this answer
























          • No, it's not. This should be done e.g. via a Style-trigger or a bound property.

            – Lennart
            Nov 21 '18 at 8:13
















          -2














          it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function






          share|improve this answer
























          • No, it's not. This should be done e.g. via a Style-trigger or a bound property.

            – Lennart
            Nov 21 '18 at 8:13














          -2












          -2








          -2







          it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function






          share|improve this answer













          it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 8:04









          LemonsLemons

          47110




          47110













          • No, it's not. This should be done e.g. via a Style-trigger or a bound property.

            – Lennart
            Nov 21 '18 at 8:13



















          • No, it's not. This should be done e.g. via a Style-trigger or a bound property.

            – Lennart
            Nov 21 '18 at 8:13

















          No, it's not. This should be done e.g. via a Style-trigger or a bound property.

          – Lennart
          Nov 21 '18 at 8:13





          No, it's not. This should be done e.g. via a Style-trigger or a bound property.

          – Lennart
          Nov 21 '18 at 8:13


















          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%2f53407526%2fwpf-button-in-datagrid-visibility%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]