绑定到函数返回

本文关键字:返回 函数 绑定 | 更新日期: 2023-09-27 18:13:20

我有一个"Checked ListBox",一个ListBox和一个CheckBox为我的每个项目。
在这个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>

ViewModel:

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,其中包含PlayersHashSet称为Participants
我想将我的IsChecked属性绑定到MyTournament.Participants.Contains(this)的结果,这是与CheckBox相关的Player。但我没法让它起作用。

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

<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
有什么建议吗?

绑定到函数返回

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

列表框:

<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();
    }
}