WPF CheckBox 復選框綁定 Binding


<Window.DataContext>
    <local:VMTempTest/>
</Window.DataContext>
<Grid>
    <StackPanel Margin="10">
        <TextBlock Text="復合框" FontWeight="Bold"  Margin="0,5,0,5" ></TextBlock>
        <DockPanel x:Name="GroupCheckButton" >
            <StackPanel DockPanel.Dock="Left">
                <ItemsControl ItemsSource="{Binding CheckButtons}" x:Name="cbt" >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Content}" IsChecked="{Binding IsCheck}"
                                             Command="{Binding DataContext.CheckCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ItemsControl}}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>

            <StackPanel DockPanel.Dock="Right" Margin="50 0 0 0" VerticalAlignment="Center" Orientation="Horizontal">
                <TextBlock Text="{Binding CheckInfo,StringFormat='結果:\{0\}'}" ></TextBlock>
            </StackPanel>
        </DockPanel>
    </StackPanel>
</Grid>

Models:

public class CompBottonModel : ObservableObject
{
    public CompBottonModel()
    {
        //構造函數
    }

    private String content;
    /// <summary>
    /// 單選框相關
    /// </summary>
    public String Content
    {
        get { return content; }
        set { content = value; RaisePropertyChanged(() => Content); }
    }


    private Boolean isCheck;
    /// <summary>
    /// 單選框是否選中
    /// </summary>
    public Boolean IsCheck
    {
        get { return isCheck; }
        set { isCheck = value; RaisePropertyChanged(() => IsCheck); }
    }
}

ViewModel:

public class VMTempTest : ViewModelBase
{
    public VMTempTest()
    {
        CheckButtons = new List<CompBottonModel>()
        {
             new CompBottonModel(){ Content="蘋果", IsCheck = false },
             new CompBottonModel(){ Content="香蕉", IsCheck = false },
             new CompBottonModel(){ Content="梨", IsCheck = false },
             new CompBottonModel(){ Content="櫻桃", IsCheck = false }
        };
    }

    private List<CompBottonModel> checkButtons;
    /// <summary>
    /// 組合復選框
    /// </summary>
    public List<CompBottonModel> CheckButtons
    {
        get { return checkButtons; }
        set
        {
            checkButtons = value; RaisePropertyChanged(() => CheckButtons);
        }
    }

    private String checkInfo;
    /// <summary>
    /// 確認框選中信息
    /// </summary>
    public String CheckInfo
    {
        get { return checkInfo; }
        set { checkInfo = value; RaisePropertyChanged(() => CheckInfo); }
    }

    private RelayCommand checkCommand;
    /// <summary>
    /// 復選框命令
    /// </summary>
    public RelayCommand CheckCommand
    {
        get
        {
            if (checkCommand == null)
                checkCommand = new RelayCommand(() => ExcuteCheckCommand());
            return checkCommand;

        }
        set { checkCommand = value; }
    }

    private void ExcuteCheckCommand()
    {
        CheckInfo = "";
        if (CheckButtons != null && CheckButtons.Count > 0)
        {
            var list = CheckButtons.Where(p => p.IsCheck);
            if (list.Count() > 0)
            {
                foreach (var l in list)
                {
                    CheckInfo += l.Content + ",";
                }

                CheckInfo = CheckInfo.TrimEnd(',');  // 把最后一個逗號刪掉
            }
        }
    }
}




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM