WPF 绑定 Grid 上的 SharedSizeGroup 不起作用

发布于 2024-11-17 02:48:09 字数 536 浏览 2 评论 0原文

我正在开发一个 WPF 应用程序来练习 MVVM。我需要有 4 个网格,所有网格都只有两列 - 一列用于“显示”控件(带有字段名称的 TextBlock/RadioButton),另一列用于“值”控件(表示字段值所需的任何控件) )。

另一个用户控件中的每个网格,我需要使它们的所有第一列保持同步,因此“值”控件将在屏幕上延伸,而共享大小的“显示”控件将具有不改变的自动宽度。

如果我使用常量名称设置 SharedSizeColumn,则所有网格都处于完美且美观的同步状态,但我需要通过与视图模型绑定来设置 SharedSizeColumn,因为包含这些网格的某些用户控件在选项卡式视图模型之间共享以供重用,跨选项卡/视图模型我不希望网格同步。 当我使用绑定设置 SharedSizeGroup 时,所有网格中的 2 列就像根本没有设置 SharedSizeGroup 一样,我什至尝试使用 BindingOperations 通过代码设置绑定,但仍然没有运气。

知道如何成功绑定 SharedSizeGroup,或者防止 SharedSizeGroup 在重用相同用户控件的选项卡之间共享的其他解决方案吗?

I am working on a WPF application to practice MVVM. I have a need to have 4 grids, all have only two columns - one for a "display" control (TextBlock/RadioButton with a field's name) and the other for "value" control (any control needed to represent the value of the field).

Each grid in another user control, and I need to have all of their first columns to be in sync, so the "value" controls will stretch over the screen while the "display" controls that share size will have automatic width that doesnt change.

If I set SharedSizeColumn with constant name, all the grids are in perfect and good looking sync, but I need to set the SharedSizeColumn via binding with my view model, because some user controls that contain those grids are shared between tabbed view models for reuse, and across tabs/view models I dont want the grids to be synchronized.
When I set SharedSizeGroup with bindings, the 2 columns in all the grids act like there is no SharedSizeGroup set at all, I've even tried setting the binding via code using BindingOperations and still no luck.

Any idea how to successfully bind SharedSizeGroup, or another solution for preventing the SharedSizeGroup to be shared amongst tabs that reuse the same user control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-11-24 02:48:09

以下是使用 SharedSizeGroup 进行数据绑定的完整工作示例。

标记:

<Grid>
    <StackPanel Grid.IsSharedSizeScope="True" Margin="20">
        <Grid HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnA}"/>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnB}"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="aa" Grid.Column="0" Foreground="Red"/>
            <TextBlock Text="bbbbbbbb" Grid.Column="1" Foreground="Blue"/>
        </Grid>
        <Grid HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnC}"/>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnD}"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="cccccccc" Grid.Column="0" Foreground="Red"/>
            <TextBlock Text="dd" Grid.Column="1" Foreground="Blue"/>
        </Grid>
    </StackPanel>
</Grid>

和代码隐藏:

void Window_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = new SharedSizeGroupViewModel
    {
        ColumnA = "group1",
        ColumnB = "group2",
        ColumnC = "group1",
        ColumnD = "group2",
    };
}

以及原始视图模型:

public class SharedSizeGroupViewModel
{
    public string ColumnA { get; set; }
    public string ColumnB { get; set; }
    public string ColumnC { get; set; }
    public string ColumnD { get; set; }
}

这就是它的样子:

SharedSizeGroup Demo

它显示了红色和蓝色的柱子排列在一起。

Here is a complete working sample of using SharedSizeGroup with data binding.

The markup:

<Grid>
    <StackPanel Grid.IsSharedSizeScope="True" Margin="20">
        <Grid HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnA}"/>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnB}"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="aa" Grid.Column="0" Foreground="Red"/>
            <TextBlock Text="bbbbbbbb" Grid.Column="1" Foreground="Blue"/>
        </Grid>
        <Grid HorizontalAlignment="Left">
            <Grid.ColumnDefinitions>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnC}"/>
                <ColumnDefinition SharedSizeGroup="{Binding ColumnD}"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="cccccccc" Grid.Column="0" Foreground="Red"/>
            <TextBlock Text="dd" Grid.Column="1" Foreground="Blue"/>
        </Grid>
    </StackPanel>
</Grid>

and the code-behind:

void Window_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = new SharedSizeGroupViewModel
    {
        ColumnA = "group1",
        ColumnB = "group2",
        ColumnC = "group1",
        ColumnD = "group2",
    };
}

and the primitive view-model:

public class SharedSizeGroupViewModel
{
    public string ColumnA { get; set; }
    public string ColumnB { get; set; }
    public string ColumnC { get; set; }
    public string ColumnD { get; set; }
}

and this is what it looks like:

SharedSizeGroup Demo

which shows the red and blue columns lined up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文