WPF. Button in DataGrid Visibility
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
add a comment |
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
add a comment |
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
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
c# wpf button
edited Nov 21 '18 at 8:06
Foo
1
1
asked Nov 21 '18 at 7:59
Дмитрий СуворовДмитрий Суворов
236
236
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
Yes, Thank you. I already did the same
– Дмитрий Суворов
Nov 21 '18 at 10:05
add a comment |
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.
I'm not very strong yet about C#. I tryed to use your code. I changedConverter={StaticResource TextToVisibilityConverter}}
toConverter={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
add a comment |
it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Yes, Thank you. I already did the same
– Дмитрий Суворов
Nov 21 '18 at 10:05
add a comment |
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
Yes, Thank you. I already did the same
– Дмитрий Суворов
Nov 21 '18 at 10:05
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
I'm not very strong yet about C#. I tryed to use your code. I changedConverter={StaticResource TextToVisibilityConverter}}
toConverter={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
add a comment |
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.
I'm not very strong yet about C#. I tryed to use your code. I changedConverter={StaticResource TextToVisibilityConverter}}
toConverter={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
add a comment |
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.
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.
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 changedConverter={StaticResource TextToVisibilityConverter}}
toConverter={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
add a comment |
I'm not very strong yet about C#. I tryed to use your code. I changedConverter={StaticResource TextToVisibilityConverter}}
toConverter={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
add a comment |
it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function
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
add a comment |
it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function
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
add a comment |
it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function
it's better to use trigger function from onChange() event (example : textbox.onChange()) and change the visibility (button.hidden = true/false) inside trigger function
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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