WPF GUI 和几个列表框/复选框之间的数据绑定

发布于 2024-10-06 00:01:05 字数 3755 浏览 7 评论 0原文

我想做的事情看起来很简单,但花了我很多时间却没有任何结果..=/ 我想要一个 WPF 窗口,它接受具有某些属性的对象并用它来填充几个项目。 具体来说,收到的对象是这样定义的:

public class ParameterForGraphicOptions
{
    public List<VariablesOptions> Variables { get; set; }
    public List<string> Simulations { get; set; }
    public List<string> ShowSimulations { get; set; }
}
public class VariablesOptions
{
    public string Name { get; set; }
    public bool Show { get; set; }
    public bool Average { get; set; }
    public bool Var { get; set; }
}

我想用模拟和 ShowSimulations 填充 2 个列表框,并且还有另一个连接到变量名称和 3 个复选框的列表,当您更改中的所选项目时,它们会更改它们的值列表..Windows代码(.cs)如下:

public GraphicOptions(ParameterForGraphicOptions pfgo) //NAME OF THE WINDOW
{
    InitializeComponent();                     //STD CALL
    this.DataContext = pfgo;                   //CONNECTING THE DATA CONTEXT
}

用于绑定的XAML代码是这样的:

<Window x:Class="GUI.GraphicOptions"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GraphicOptions" Height="450" Width="350">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="150"/>
            <RowDefinition Height="25" />
            <RowDefinition Height="150"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25" />
            <RowDefinition Height="11*" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0">Simulation in Progress</Label>
        <Label Margin="188,0,0,0">Simulation to Show</Label>
        <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Name="Simulations" VerticalAlignment="Top" Width="140" />  
        <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Margin="188,0,0,0" Name="SimulationsToShow" VerticalAlignment="Top" Width="140" />
        <Label Grid.Row="2">Variables to Show</Label>

        <ListBox Grid.Column="0" Grid.Row="3" DataContext="{Binding Variables.Name}" Height="150" HorizontalAlignment="Left" Name="Variables" VerticalAlignment="Top" Width="140"  Grid.RowSpan="2" />
        <CheckBox Grid.Row="3" Content="Show" IsChecked="{Binding Variables.Show}"  Height="20" HorizontalAlignment="Left" Name="Show" VerticalAlignment="Top" Width="76" Margin="163,5,0,0" />
        <CheckBox Grid.Row="3" Content="Average" IsChecked="{Binding Variables.Average}"  Height="25"  HorizontalAlignment="Left" Name="Average" VerticalAlignment="Top" Width="76" Margin="174,54,0,0" Checked="Average_Checked" />
        <CheckBox Grid.Row="3" Content="Var" IsChecked="{Binding Variables.Var}" Height="25"  HorizontalAlignment="Left" Name="Variance" VerticalAlignment="Top" Width="76" Margin="163,31,0,0" />
        <CheckBox Content="Refresh Graphic During Computation" Grid.Row="5" Height="25" HorizontalAlignment="Left" Name="Continuos" VerticalAlignment="Top" Width="220" />
        <Button Content="Save" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="253,1,0,0" Name="Save" VerticalAlignment="Top" Width="75" Click="Save_Click" />
        <Button Content="->" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,30,0,0" Name="OneSimulation" VerticalAlignment="Top" Width="35" />
        <Button Content="=>" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,65,0,0" Name="AllSimulation" VerticalAlignment="Top" Width="35" />
    </Grid>
</Window>

我尝试了很多方法,但总是只绑定一个项目,所以我无法理解这个库是如何工作的。 非常感谢 4 all :P

问题是:我发布的代码中有什么错误吗?

PS:对不起我的英语:P

The thing i want to do looks easy, but takes me a lot of time without any result.. =/
I'd like to have a WPF window which takes an object with some proprieties and used it to popolate a couple of item.
In the specific the object recieved is so defined:

public class ParameterForGraphicOptions
{
    public List<VariablesOptions> Variables { get; set; }
    public List<string> Simulations { get; set; }
    public List<string> ShowSimulations { get; set; }
}
public class VariablesOptions
{
    public string Name { get; set; }
    public bool Show { get; set; }
    public bool Average { get; set; }
    public bool Var { get; set; }
}

And i'd like to populate 2 ListBox with Simulations and ShowSimulations, and also have another list which is connected to Variables Name and 3 check box which change their values when you change the selected item in the list.. The Windows code (.cs) follows:

public GraphicOptions(ParameterForGraphicOptions pfgo) //NAME OF THE WINDOW
{
    InitializeComponent();                     //STD CALL
    this.DataContext = pfgo;                   //CONNECTING THE DATA CONTEXT
}

The XAML code for binding is this:

<Window x:Class="GUI.GraphicOptions"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="GraphicOptions" Height="450" Width="350">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="150"/>
            <RowDefinition Height="25" />
            <RowDefinition Height="150"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25" />
            <RowDefinition Height="11*" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0">Simulation in Progress</Label>
        <Label Margin="188,0,0,0">Simulation to Show</Label>
        <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Name="Simulations" VerticalAlignment="Top" Width="140" />  
        <ListBox Grid.Row="1" Height="150" HorizontalAlignment="Left" Margin="188,0,0,0" Name="SimulationsToShow" VerticalAlignment="Top" Width="140" />
        <Label Grid.Row="2">Variables to Show</Label>

        <ListBox Grid.Column="0" Grid.Row="3" DataContext="{Binding Variables.Name}" Height="150" HorizontalAlignment="Left" Name="Variables" VerticalAlignment="Top" Width="140"  Grid.RowSpan="2" />
        <CheckBox Grid.Row="3" Content="Show" IsChecked="{Binding Variables.Show}"  Height="20" HorizontalAlignment="Left" Name="Show" VerticalAlignment="Top" Width="76" Margin="163,5,0,0" />
        <CheckBox Grid.Row="3" Content="Average" IsChecked="{Binding Variables.Average}"  Height="25"  HorizontalAlignment="Left" Name="Average" VerticalAlignment="Top" Width="76" Margin="174,54,0,0" Checked="Average_Checked" />
        <CheckBox Grid.Row="3" Content="Var" IsChecked="{Binding Variables.Var}" Height="25"  HorizontalAlignment="Left" Name="Variance" VerticalAlignment="Top" Width="76" Margin="163,31,0,0" />
        <CheckBox Content="Refresh Graphic During Computation" Grid.Row="5" Height="25" HorizontalAlignment="Left" Name="Continuos" VerticalAlignment="Top" Width="220" />
        <Button Content="Save" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="253,1,0,0" Name="Save" VerticalAlignment="Top" Width="75" Click="Save_Click" />
        <Button Content="->" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,30,0,0" Name="OneSimulation" VerticalAlignment="Top" Width="35" />
        <Button Content="=>" Grid.Row="1" Height="35" HorizontalAlignment="Left" Margin="145,65,0,0" Name="AllSimulation" VerticalAlignment="Top" Width="35" />
    </Grid>
</Window>

I tried in many ways, but always bind only a item, so I can't understand HOW this library works..
Many thanks 4 all :P

The question is: is there any wrong thing in the code i posted?

P.S.: Sorry for my English :P

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

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

发布评论

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

评论(1

唐婉 2024-10-13 00:01:05

不要将单个控件与多个项目绑定。与单个选定项目绑定。

这是一个正确的解决方案:

<ListBox x:Name="vars" ItemsSource="{Binding Variables}" DisplayMemberPath="Name" />
<CheckBox IsChecked="{Binding SelectedItem.Show, ElementName=vars, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding SelectedItem.Average, ElementName=vars, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding SelectedItem.Var, ElementName=vars, Mode=TwoWay}" />

Don't bind a single control with plural items. Bind with a single selected item.

Here is a correct solution:

<ListBox x:Name="vars" ItemsSource="{Binding Variables}" DisplayMemberPath="Name" />
<CheckBox IsChecked="{Binding SelectedItem.Show, ElementName=vars, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding SelectedItem.Average, ElementName=vars, Mode=TwoWay}" />
<CheckBox IsChecked="{Binding SelectedItem.Var, ElementName=vars, Mode=TwoWay}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文