繁体   English   中英

绑定到背后的代码

[英]Bind to code behind

我正在将一个非常老的控件改编为MVVM类型的控件。 我有警报列表。 当用户按下列标题中的按钮时,我必须清除可见警报列表并滚动到下一个警报(因此第一个不可见的警报)。

我在列标题的控件模板中创建了按钮。 command属性有效,但是它返回NaN,因此我希望将command参数绑定到窗口的可见部分的Height不正确。 当我调试后面的代码时,属性“ 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,
                });
            }

        }
     }

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

   public AlarmList()
    {
        InitializeComponent();

        Height = this.ActualHeight;
    }

或者像这样更改您的CommandParameter

 CommandParameter="{Binding ActualHeight .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM