WPF 移动对象 Grid.Row?
WPF Moving object by Grid.Row?
我有一个问题,如何在 WPF 应用程序中按行移动对象?我的 Xaml 看起来像这样:
<Grid>
<Grid.ColumnDefinitions>
<Some columns here!/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Some Rows here!/>
</Grid.RowDefinitions>
<Button x:Name="Button1"
Grid.Row="1" Grid.Column="4"
Width="50" Height="50"
Content="Button1"
Foreground="White"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
BorderBrush="{x:Null}"
Background="#FF085078"/>
<Button x:Name="Button2"
Grid.Row="2" Grid.Column="4"
Content="Button2"
Foreground="White"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
BorderBrush="{x:Null}"
Background="#FF085078"/>
<ContentControl Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="3"
x:Name="ActiveItem"/>
我想做的是,当我在内容控件中显示另一个 window 时,我想将 "Button2" 移动到内容控件的第二个视图下。如何在内容控制下移动项目,当我关闭第二个视图时,项目又回到它们的主要位置?如何绑定 Grid.Row 来做我想做的事?如果有帮助,我正在使用 Caliburn.Micro Framework>
感谢您的指点!
首先将每个 Grid.Row
绑定到一个可绑定的 属性。
<Grid>
<Grid.ColumnDefinitions>
<Some columns here!/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Some Rows here!/>
</Grid.RowDefinitions>
<Button x:Name="Button1"
Grid.Row="{Binding Button1Row}" Grid.Column="4"
Width="50" Height="50"
Content="Button1" />
<Button x:Name="Button2"
Grid.Row="{Binding Button2Row}" Grid.Column="4"
Width="50" Height="50"
Content="Button2"/>
<ContentControl Grid.Row="{Binding ContentControlRow}" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="3"
x:Name="ActiveItem"/>
</Grid>
现在在您的 DataContext 绑定对象中,您需要定义这些可绑定属性。
请注意,在 MVVM 中,行号可能被视为 view 而不是 ViewModel 。所以首先你需要决定你想走哪条路。
您可以在 ViewModel 中添加这些可绑定属性,并通过 命令.
修改它们
您可以直接将它们添加到查看的代码隐藏(MyWindow.xaml.cs或MyUserControl.xaml.cs)并通过事件处理器.
第二种情况:
您可能会忘记绑定,只需按照 Clemens 的建议使用 Grid.SetRow(Button2, 3);
。
或者您可以使用具有以下修改的绑定:
- 将托管网格的用户控件(或window)的
x:Name
设置为,例如MyUserControlRoot
.
- 将
ElementName
添加到绑定。 例如Grid.Row="{Binding ElementName=MyUserControlRoot, Path=Button1Row}"
您可以声明一个按钮样式,根据 ContentControl 的 Content
是否为空来设置 Grid.Row
属性 值:
<Button Content="Button2" Grid.Column="4">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Grid.Row" Value="3"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Content, ElementName=ActiveItem}"
Value="{x:Null}">
<Setter Property="Grid.Row" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
如果已经有
指定的样式
<Button Style="{StaticResource X}" .../>
删除它并写这个:
<Button ...>
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource X}">
...
</Style>
</Button.Style>
</Button>
我有一个问题,如何在 WPF 应用程序中按行移动对象?我的 Xaml 看起来像这样:
<Grid>
<Grid.ColumnDefinitions>
<Some columns here!/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Some Rows here!/>
</Grid.RowDefinitions>
<Button x:Name="Button1"
Grid.Row="1" Grid.Column="4"
Width="50" Height="50"
Content="Button1"
Foreground="White"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
BorderBrush="{x:Null}"
Background="#FF085078"/>
<Button x:Name="Button2"
Grid.Row="2" Grid.Column="4"
Content="Button2"
Foreground="White"
BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
BorderBrush="{x:Null}"
Background="#FF085078"/>
<ContentControl Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="3"
x:Name="ActiveItem"/>
我想做的是,当我在内容控件中显示另一个 window 时,我想将 "Button2" 移动到内容控件的第二个视图下。如何在内容控制下移动项目,当我关闭第二个视图时,项目又回到它们的主要位置?如何绑定 Grid.Row 来做我想做的事?如果有帮助,我正在使用 Caliburn.Micro Framework>
感谢您的指点!
首先将每个 Grid.Row
绑定到一个可绑定的 属性。
<Grid>
<Grid.ColumnDefinitions>
<Some columns here!/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<Some Rows here!/>
</Grid.RowDefinitions>
<Button x:Name="Button1"
Grid.Row="{Binding Button1Row}" Grid.Column="4"
Width="50" Height="50"
Content="Button1" />
<Button x:Name="Button2"
Grid.Row="{Binding Button2Row}" Grid.Column="4"
Width="50" Height="50"
Content="Button2"/>
<ContentControl Grid.Row="{Binding ContentControlRow}" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="3"
x:Name="ActiveItem"/>
</Grid>
现在在您的 DataContext 绑定对象中,您需要定义这些可绑定属性。
请注意,在 MVVM 中,行号可能被视为 view 而不是 ViewModel 。所以首先你需要决定你想走哪条路。
您可以在 ViewModel 中添加这些可绑定属性,并通过 命令.
修改它们您可以直接将它们添加到查看的代码隐藏(MyWindow.xaml.cs或MyUserControl.xaml.cs)并通过事件处理器.
第二种情况:
您可能会忘记绑定,只需按照 Clemens 的建议使用
Grid.SetRow(Button2, 3);
。或者您可以使用具有以下修改的绑定:
- 将托管网格的用户控件(或window)的
x:Name
设置为,例如MyUserControlRoot
. - 将
ElementName
添加到绑定。 例如Grid.Row="{Binding ElementName=MyUserControlRoot, Path=Button1Row}"
- 将托管网格的用户控件(或window)的
您可以声明一个按钮样式,根据 ContentControl 的 Content
是否为空来设置 Grid.Row
属性 值:
<Button Content="Button2" Grid.Column="4">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Grid.Row" Value="3"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Content, ElementName=ActiveItem}"
Value="{x:Null}">
<Setter Property="Grid.Row" Value="2"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
如果已经有
指定的样式<Button Style="{StaticResource X}" .../>
删除它并写这个:
<Button ...>
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource X}">
...
</Style>
</Button.Style>
</Button>