Search code examples
c#wpfxamlwpf-controlsdatacontext

Button placed within a Grid's column is not clickable or changing color when hovering over it


So I am fairly new to WPF and XAML in general, and I am having a problem with one of my controls(a button). For some background, I have a XAML file with this grid structure:

<Grid.ColumnDefinitions>
<ColumnDefinitions Width=Auto />
<ColumnDefinitions Width=Auto />
<ColumnDefinitions Width=Auto />
<ColumnDefinitions Width=Auto />
<ColumnDefinitions Width=* />
<ColumnDefinitions Width=Auto />
<ColumnDefinitions Width=Auto />
<Grid.ColumnDefinitions>

So, in the XAML code each Grid.Column is assigned a different control (ContentControl, DropDown, ...) However, when I place two buttons within either a DockPanel or StackPanel and assign the DockPanel or StackPanel a Grid.Column, when I hover over the buttons they do not start changing to light blue (which I guess is the default MouseOver color) nor are they clickable. Each button is tied to a DataContext that works when the StackPanel or DockPanel of buttons is not assigned a Grid.Column (it is basically pushed to the left of the grid within the form it is contained in). However, when it is given a GridColumn number and when the preceding GridColumn element has starsizing, the buttons are not clickable and do not change color at all when mouse hovers over them. I tried everything I can think of (changing Zindex in XAML, experimenting with sizing options for griddefinitions, ...). Again I am very new to XAML and WPF. Does anyone have an idea of what I could be missing?

Here is an example of the GridColumn Definitions:

<ContentControl Style="{StaticResource ....}"
Grid.Column="4"
Content="{Binding .....}"/>

<DockPanel Grid.Column="5"
  <Button Style="{StaticResource ....}"
  Command = {Binding Path = DataContext.somecommandname, 
  RelativeSource="..."
  CommandParameter="{Binding"}
  Visibility=""
/>
<Button ...../>
</DockPanel>

So, basically the above ContentSource has a maxwidth of about 500 px defined in a resource file, and it has starsizing (*). However, the buttons do not seem to like coming behind ANY element with starsizing(I tried various elements), and will not function.

** I have edited the question with a fuller version of the xaml code in question. Any help would be really appreciated!

<ResourceDictionary
             ....
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary 
        .......

    <DataTemplate DataType="">
        <Border BorderBrush="CornflowerBlue" BorderThickness="0.5">
            <Grid HorizontalAlignment="Stretch" Style="{StaticResource GridMarginStyle}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />  
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <ContentControl Content="{Binding StatusMessage.Severity}"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Margin="5,0,5,0"
                                                />
                <TextBox Style="{StaticResource BarcodeTextBoxStyle}" Grid.Column="1"  ... />
                <ComboBox Style="{StaticResource ItemsComboBoxStyle}" Grid.Column="2"
                          ItemsSource="{Binding ItemTypes}"
                          SelectedItem="{Binding SelectedItemType}"
                          DisplayMemberPath="Name"
                          IsSynchronizedWithCurrentItem="True"
                          IsEnabled="{Binding IsEditable}"
                          SelectedIndex="-1"
                                      />
                <ContentControl Style="{StaticResource StatusMessageStyle}"
                                Grid.Column="3"
                                Background="Red"
                                Content="{Binding StatusMessage}"/>


                <DockPanel Grid.Column="4" >
                    <Button Style="{StaticResource ViewButtonStyle}"
                         
                                    Command="{Binding Path = DataContext.FormItemCommand,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcontrol:ControlXItems}}}"
                        CommandParameter="{Binding}"
                        Visibility="{Binding IsViewable, Converter={x:Static converters:BoolToVisibilityConverter.FalseToCollapsed}}"
                           
                           
                            />
                    <Button Style="{StaticResource ViewOpenURLButtonStyle}"
                           
                                    Command="{Binding Path = DataContext.FormURLItemCommand,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type x:ControlxItems}}}"
                        CommandParameter="{Binding}"
                        Visibility="{Binding IsViewable, Converter={x:Static converters:BoolToVisibilityConverter.FalseToCollapsed}}"
                            />
               </DockPanel>

                <Button Style="{StaticResource DeleteButtonStyle}" Grid.Column="5"
                                    Command="{Binding Path=DataContext.RemoveFormItemCommand,
                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type x:ControlxItems}}}"
                        CommandParameter="{Binding}"
                                    Visibility="{Binding ItemProcessingAvailable, Converter={x:Static converters:BoolToVisibilityConverter.FalseToCollapsed}}"
                        />
             
                        
            </Grid>
        </Border>
    </DataTemplate>
</ResourceDictionary>

Solution

  • I figured out the problem, apparently this is just a Visual Studio issue, when the .exe is created of the application, there are no button issues whatsoever and everything works fine. Not sure why, but in case anyone has this issue it may not actually be a problem.