2017-11-24 3 views
0

私は非常に古いコントロールをMVVMのようなコントロールに戻しています。私は警報のリストを持っています。ユーザーが列見出しのボタンを押すと、表示されているアラームのリストをクリアし、次のアラームにスクロールする必要があります(最初のアラームは表示されません)。コードの後ろにバインド

私は、列見出しのコントロールテンプレートにボタンを作成しました。コマンドプロパティは機能しますが、NaNが返されるため、コマンドパラメータのウィンドウの表示部分の高さへのバインディングが正しくないことが予想されます。私がコードをデバッグするとき、プロパティ "高さ"は数字を保持します。

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:データグリッド

{プライベートダブル高さ= 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, 
       }); 
      } 

     } 
    } 
+0

「ActualHeight」にバインドしよう – Mitya

答えて

1

私はあなたがユーザーコントロールを使用してプロパティを初期化する場合、それは

public AlarmList() 
    { 
     InitializeComponent(); 

     Height = this.ActualHeight; 
    } 

またはこのようなあなたのCommandParameterを変更する作品になると思う。

CommandParameter="{Binding ActualHeight ..... 
+1

2行のコード行をポストするのではなく、答えに説明を追加する必要があります。 – dymanoid

+0

あなたはそうです、私は微調整して答えを改善しています。 –

+0

それは魅力のように働いた、ありがとう。しかし、なぜ私はこのように機能し、高さを拘束していますか? –

関連する問題