C# WPF-项模板的网格使用 我想把按钮绑定到我的自定义字段类型 kfield < /代码>,并且我需要5个按钮放置在x的形状中,所以在3x3网格上,我需要4个边和中间的按钮。我有以下字段类型: public class KField : ViewModelBase // ViewModelBase is a custom abstract INotifyPropertyChanged { private char _text; private bool _isEnabled; private int _x; private int _y; public int X { get { return _x; } set { _x = value; OnPropertyChanged(); } } public int Y { get { return _y; } set { _y = value; OnPropertyChanged(); } } public bool IsEnabled { get { return _isEnabled; } set { if (_isEnabled != value) { _isEnabled = value; OnPropertyChanged(); } } } public char Text { get { return _text; } set { if (_text != value) { _text = value; OnPropertyChanged(); } } } public DelegateCommand ClickCommand { get; set; } }

C# WPF-项模板的网格使用 我想把按钮绑定到我的自定义字段类型 kfield < /代码>,并且我需要5个按钮放置在x的形状中,所以在3x3网格上,我需要4个边和中间的按钮。我有以下字段类型: public class KField : ViewModelBase // ViewModelBase is a custom abstract INotifyPropertyChanged { private char _text; private bool _isEnabled; private int _x; private int _y; public int X { get { return _x; } set { _x = value; OnPropertyChanged(); } } public int Y { get { return _y; } set { _y = value; OnPropertyChanged(); } } public bool IsEnabled { get { return _isEnabled; } set { if (_isEnabled != value) { _isEnabled = value; OnPropertyChanged(); } } } public char Text { get { return _text; } set { if (_text != value) { _text = value; OnPropertyChanged(); } } } public DelegateCommand ClickCommand { get; set; } },c#,wpf,xaml,C#,Wpf,Xaml,我使用一个\u fields变量,它包含所有按钮并绑定到视图: _fields = new ObservableCollection<KField>(); _fields.Add(new KField { Text = _model.ModelFields[0], IsEnabled = true, X = 0, Y = 0}); _fields.Add(new KField { Text = _model.ModelFields[1], IsEnabled = false, X =

我使用一个
\u fields
变量,它包含所有按钮并绑定到视图:

_fields = new ObservableCollection<KField>();
_fields.Add(new KField { Text = _model.ModelFields[0], IsEnabled = true, X = 0, Y = 0});
_fields.Add(new KField { Text = _model.ModelFields[1], IsEnabled = false, X = 0, Y = 2 });
_fields.Add(new KField { Text = _model.ModelFields[2], IsEnabled = false, X = 1, Y = 1 });
_fields.Add(new KField { Text = _model.ModelFields[3], IsEnabled = true, X = 2, Y = 0 });
_fields.Add(new KField { Text = _model.ModelFields[4], IsEnabled = false, X = 2, Y = 2 });
我正在使用这个XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="376" Width="534">
    <Grid x:Name="myGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ItemsControl ItemsSource="{Binding Fields}" Margin="0,0,0,35">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Grid.Column="{Binding Y}" Grid.Row="{Binding X}" BorderThickness="0.3 " BorderBrush="Black" Command="{Binding ButtonPressed}" Content="{Binding Text}" IsEnabled="{Binding IsEnabled}">
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>


但是,这会将我的所有按钮放入网格的第一个单元格中。文本和启用/禁用状态工作正常,因此绑定确实发生,但它忽略所有X和Y属性。我错过了什么?我怎样才能按我想要的方式放置这些按钮?谢谢大家!

问题是,
ItemsControl
只是与网格分离。如果我在
ItemsControl
中放置一个网格,并在
itemscontainerstyle
中提供坐标的绑定,它可以正常工作:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="376" Width="534">
    <ItemsControl x:Name="MyItems" ItemsSource="{Binding Fields}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button BorderThickness="0.3 " BorderBrush="Black" Command="{Binding ButtonPressed}" Content="{Binding Text}" IsEnabled="{Binding IsEnabled}">
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Style.Setters>
                    <Setter Property="Grid.Row" Value="{Binding X}" />
                    <Setter Property="Grid.Column" Value="{Binding Y}" />
                </Style.Setters>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Window>


我不知道我是否正确,但我认为您需要将网格放在
ItemsControl
中,甚至可能放在
DataTemplate
中,尽管我更不确定是否放在那里。试试看有没有什么变化?因为您现在说的是将按钮放在其祖先网格中,但是如果这有意义的话,祖先不是网格而是数据模板。看看这是否有帮助?看看这个链接,如果有帮助的话:别忘了提到ItemContainerStyle的使用,在这里你必须把X和Y绑定放在ItemTemplate中,而不是放在ItemTemplate中。我真的不知道你能做到这一点!我想我还有很多东西要学:)@Markinson哦,哈哈,我找到了与你链接的内容不同的解决方案!:D谢谢你的帮助!:)非常感谢!哦哈哈;)没问题!很高兴你找到了答案
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="376" Width="534">
    <ItemsControl x:Name="MyItems" ItemsSource="{Binding Fields}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button BorderThickness="0.3 " BorderBrush="Black" Command="{Binding ButtonPressed}" Content="{Binding Text}" IsEnabled="{Binding IsEnabled}">
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Style.Setters>
                    <Setter Property="Grid.Row" Value="{Binding X}" />
                    <Setter Property="Grid.Column" Value="{Binding Y}" />
                </Style.Setters>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Window>