【问题标题】:WPF Binding to a function returnWPF 绑定到函数返回
【发布时间】:2017-02-26 13:28:17
【问题描述】:

我的每个项目都有一个“已检查列表框”,ListBoxCheckBox
在这个ListBox 我有一个玩家列表。

<ListBox ItemsSource="{Binding MyPlayers}" SelectedItem="{Binding SelectedPlayer}" Margin="69,51,347.4,161.8">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem>
                <CheckBox IsChecked="{Binding ???}" Content="{Binding Path=Pseudo}" />
            </ListBoxItem>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

视图模型:

public class MainWindowViewModel : BaseViewModel
{
    SavingContext MyDatabaseContext;
    public ObservableCollection<Player> MyPlayers
    {
        get
        {
            return MyDatabaseContext.MyPlayers.Local;
        }
    }

    Tournament _MyTournament;
    public Tournament MyTournament
    {
        get
        {
            return _MyTournament;
        }

        set
        {
            _MyTournament = value;
        }
    }

    public MainWindowViewModel(Tournament myTournament)
    {
        MyDatabaseContext = new SavingContext();
        MyDatabaseContext.MyPlayers.Load();
        MyTournament = myTournament;
    }
}

我传入我的ViewModel 一个Tournament,其中包含一个HashSetPlayers,称为Participants
我想将我的IsChecked 属性绑定到MyTournament.Participants.Contains(this) 的结果,这是与CheckBox 相关的Player。但我无法让它发挥作用。

编辑:
我尝试使用转换器,但没有成功。 &lt;helper:PlayerToTournamentRegistered x:Key="PlayerToTournamentRegistered" /&gt;

<ListBoxItem>
    <CheckBox IsChecked="{Binding Converter={StaticResource PlayerToTournamentRegistered}}" Content="{Binding Path=Pseudo}" />
</ListBoxItem>

public class PlayerToTournamentRegistered : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Temp test to see if it would work
        if (value == null) return false;

        return true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

每次Provide value on 'System.Windows.Data.Binding' threw an exception我都会出错
有什么建议吗?

【问题讨论】:

  • 如果没有一个很好的minimal reproducible example 可以清楚地显示您尝试过什么以及哪些无效,就不可能提供任何建议。数据绑定要求很简单:属性更改通知支持的公共属性。转换器必须在不引发异常的情况下工作,并且转换器输入仍然必须具有属性更改通知,因此 WPF 知道何时运行转换器。如果这不能让您重回正轨,请修正您的问题,以便包含所需的信息。
  • 我不知道如果有一个最小的、完整的和可验证的示例、适用于经典的 MVVM 实现以及实体框架结构(我详细说明)会缺少什么。您可以在新项目中随意添加其余部分以获得相同的可重现示例。
  • “我不知道有一个最小、完整和可验证的例子会缺少什么”——那么你还没有阅读minimal reproducible example 页面。另请参阅How to Ask 页面底部链接的文章(当然,请确保您也阅读了该文章)。至于缺少什么,您甚至都懒得包含TournamentPlayer 类,更不用说提供一个完整minimal 的代码示例。
  • 抱歉不同意,唯一重要的是我的 Tournament 包含一个玩家的 HashSet,minimal,玩家类无关紧要,(只要它包含如果您真的要复制整个项目,则为字段伪),我将其省略以保持代码最少。我仍然不明白为什么它不能重现,我不会提供所有基本和无用的信息,因为它会扰乱人们对主要问题的关注。是的,我可以解释我如何使用实体框架,但这绝对不能帮助我解决问题。
  • “抱歉不同意”——您似乎认为您是判断您的问题是否足够质量的人。此外,您显然没有费心阅读和/或理解您可用的资源,这将指导您了解什么是足够质量的问题,因为您无法区分两者之间的区别是,和你的。祝你好运。如果您拒绝提供好的问题,您将继续得到不好的答复或没有答复。

标签: c# wpf mvvm data-binding listbox


【解决方案1】:

所以我设法使用 MultiConverter 让它工作。
我遇到的第一个错误是由于模式默认为双向,我只想让我的转换器工作单向。
我使用 MultiBinding 将我的 ListBoxItem 和我的锦标赛发送到我的转换器。
我的转换器返回真或假,并链接到我的 CheckBox.IsChecked 属性。

列表框:

<ListBox ItemsSource="{Binding MyPlayers}" SelectedItem="{Binding SelectedPlayer}" Margin="69,51,347.4,161.8">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem>
                <CheckBox Content="{Binding Path=Pseudo}" >
                    <CheckBox.IsChecked>
                        <MultiBinding Converter="{StaticResource PlayerToTournamentRegistered}" Mode="OneWay">
                            <Binding />
                            <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.MyTournament"/>
                        </MultiBinding>
                    </CheckBox.IsChecked>
                </CheckBox>
            </ListBoxItem>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

转换器:

public class PlayerToTournamentRegistered : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if(!(values[0] is Student) || !(values[1] is Tournament))
        {
            return false;
        }
        Tournament myTournament = (Tournament)values[1];
        Student myPlayer = (Student)values[0];
        if (myTournament.Participants.Contains(myPlayer))
            return true;
        return false;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多