i:InvokeCommandAction.CommandParameter中的WPF Binding.Converter只能调用一次

ff29svar  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(131)

当Grid组件大小更改时,我希望获取其ActualWidthActualHeight属性,以便可以重新计算Grid中画布的内容以适应大小。因此,以这篇文章(https://stackoverflow.com/a/72573639/9949419)为参考,我向Grid添加了一个事件触发器,并通过Converter传递其ActualWidthActualHeight属性。代码如下。
Xaml代码:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<!-- This is the Grid which I want to get its ActualWidth and ActualHeight properties.-->
<Grid x:Name="GraphicGrid">
    <!-- K value-->
    <ItemsControl ItemsSource="{Binding RectItems}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="{Binding X}"/>
                <Setter Property="Canvas.Bottom" Value="{Binding Y}"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding _Brush}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>      
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SizeChanged" >
            <i:InvokeCommandAction Command="{Binding OnSizeChanged}">
                <i:InvokeCommandAction.CommandParameter>
                    <Binding ElementName="GraphicGrid">
                        <Binding.Converter>
                            <!-- This this the value converter that passes the ActualWidth and ActualHeight properties to the event method.-->
                            <local:SizeConverter/>
                        </Binding.Converter>
                    </Binding>
                </i:InvokeCommandAction.CommandParameter>
            </i:InvokeCommandAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>        
</Grid>

查看型号代码:

Public ICommand OnSizeChanged{get;set;}
Public MainWindowViewModel()
{
    OnSizeChanged = new RelayCommand<double,double)>(Resize);
}
public void Resize((double,double)size)
{
    // TODO:
}


SizeConverter代码:

public class SizeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is FrameworkElement fe ?(fe.ActualWidth, fe.ActualHeight):(double.NaN,double.NaN);
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


当项目开始时,调用SizeConverter()并得到一个(0,0)作为结果(我假设这是加载所有元素之前Grid的默认值)。然而,在启动初始化之后,无论我如何改变窗口大小,都只调用Resize()方法。SizeConverter()不会再被调用,所以event参数仍然是(0,0)。请有人告诉我如何解决这个问题。- 谢谢-谢谢

fcwjkofz

fcwjkofz1#

MVVM中没有任何东西禁止视图调用其视图模型中的方法。
假设公共视图模型方法(如

public void Resize(double width, double height)
{
    ...
}

字符串
网格可以有一个SizeChanged事件处理程序

<Grid x:Name="GraphicGrid" SizeChanged="GraphicGrid_SizeChanged">
...


它调用视图模型方法,如

private void GraphicGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
    ((MainWindowViewModel)DataContext).Resize(e.NewSize.Width, e.NewSize.Height);
}

相关问题