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;
}
}
뷰 모델 :
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,
});
}
}
}
'ActualHeight'에 연결하려고 시도하십시오. – Mitya