0
votes

I'm reworking a very old control to a MVVM-kind of control. I have a list of alarms. When the user presses the button in the column header, I have to clear the list of visible alarms and scroll to the next alarm (so the first one which was not visible).

I created the button in the control template of the column header. The command property works but it return a NaN, so I expect that the binding of the command parameter to the Height of the visible part of the window is incorrect. When I debug the code behind, the property "Height" does hold a number.

The XAML:

<DataGrid x:Class="Kwa.Presentation.Views.AlarmList.AlarmList"
          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" 
          xmlns:local="clr-namespace:Kwa.Presentation.Views.AlarmList"
          xmlns:components="clr-namespace:Kwa.Presentation.Components"
          xmlns:converters="clr-namespace:Kwa.Presentation.Converters"
          xmlns:Trans="clr-namespace:Kwa.Presentation.Resources"
          mc:Ignorable="d" 
          d:DesignHeight="500" d:DesignWidth="750"
          ItemsSource="{Binding Alarms}"
          SelectedItem="{Binding SelectedAlarm}"
          IsSynchronizedWithCurrentItem="True"
          CanUserResizeColumns="True" IsReadOnly="True" CanUserReorderColumns="False" CanUserSortColumns="False" SelectionMode="Single" CanUserAddRows="False"
          Background="White" RowHeaderWidth="0" AutoGenerateColumns="False" GridLinesVisibility="None" RowHeight="{Binding Rowheight}" FrozenColumnCount = "1"
          ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
          x:Name="AlarmFramework"
          SizeChanged="AlarmFramework_SizeChanged"
          >

        <Style TargetType="DataGridColumnHeader" x:Key="WithButt">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="DataGridColumnHeader">
                        <Border BorderBrush="Silver" BorderThickness="0 0 0 1" 
                                Padding="5 0 0 0" Background="White">
                            <StackPanel Orientation="Horizontal" Margin="0">
                                <TextBlock Text="{Binding}" 
                              VerticalAlignment="Center" FontWeight="Bold"/>
                                <Button Content="{x:Static Trans:TranslatedResources.AlarmAcceptContent}" Margin="60 3 10 3 " VerticalAlignment="Center" VerticalContentAlignment="Center" Padding="2"
                                    Command="{Binding DataContext.AcknowledgeCommand, RelativeSource={RelativeSource AncestorType={x:Type local:AlarmList}}}" CommandParameter="{Binding Height, RelativeSource={RelativeSource AncestorType={x:Type local:AlarmList}}}" ToolTip="{x:Static Trans:TranslatedResources.AlarmAcceptTooltip}" Style="{StaticResource Butt}"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
</DataGrid>

The Code behind:

public partial class AlarmList : DataGrid

{ private double Height = 0;

public AlarmList()
{
    InitializeComponent();
}

private void AlarmFramework_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Height = e.NewSize.Height;
}

}

The ViewModel:

public class AlarmListViewModel : MainViewModelBase
    {
private readonly IActionCommand _acknowledgeCommand;
        public IActionCommand AcknowledgeCommand
        {
            get { return _acknowledgeCommand; }
        }
public AlarmListViewModel()
        {
            //Add command
            _acknowledgeCommand = new ActionCommand<double>(p => Acknowledge(p));
        }
private void Acknowledge(double parameter)
        {
            try
            {               
                double DatagridWidth = (double)parameter;
                int AmountAcknowledged = (int)Math.Floor(DatagridWidth / RowHeight);
                int LastAlarmSent = Alarms[0].AlarmNumber + AmountAcknowledged;
                _proxy.Send(LastAlarmSent);
                SelectedAlarm = Alarms[LastAlarmSent + 1];
            }
            catch (Exception ex)
            {
                _viewManager.ShowDialog(new MessageDialogViewModel()
                {
                    AskAnswer = false,
                    Text = ex.Message,
                    Title = TranslatedResources.AlarmAckSendErrorTitle,
                });
            }

        }
     }
1
try to bind to ActualHeightMitya

1 Answers

1
votes

I think if you initialize your property with usercontrol it will works

   public AlarmList()
    {
        InitializeComponent();

        Height = this.ActualHeight;
    }

Or change your CommandParameter like this:

 CommandParameter="{Binding ActualHeight .....