绑定后面的代码

时间:2017-11-24 10:07:14

标签: c# wpf xaml mvvm

我正在将一个非常旧的控件重新编写为MVVM控件。我有一个警报列表。当用户按下列标题中的按钮时,我必须清除可见警报列表并滚动到下一个警报(所以第一个警报不可见)。

我在列标题的控件模板中创建了按钮。命令属性有效,但它返回一个NaN,所以我希望命令参数绑定到窗口可见部分的高度是不正确的。当我调试后面的代码时,属性“Height”确实包含一个数字。

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>

背后的代码:

公共部分类AlarmList:DataGrid

{     private double Height = 0;

public AlarmList()
{
    InitializeComponent();
}

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

}

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 个答案:

答案 0 :(得分:1)

我认为如果使用usercontrol初始化您的属性,它将起作用

   public AlarmList()
    {
        InitializeComponent();

        Height = this.ActualHeight;
    }

或者像这样更改CommandParameter

 CommandParameter="{Binding ActualHeight .....