【问题标题】:Updating toggle buttons check state when one of them is toggled当其中一个被切换时更新切换按钮检查状态
【发布时间】:2018-12-26 06:22:26
【问题描述】:

我有一个包含 2 个字段的 XamDataGrid。在第二个字段中,我有切换按钮。单击ToggleButton 时,值会正确设置。但是在任何时候用户都应该能够在所有记录中只检查一个ToggleButton。默认属性的设置逻辑中的数据工作正常。但我需要为 DataSource 中的其他项目设置 Default 的值。

使用RadioButton 代替ToggleButton 将是理想的解决方案。但有可能没有任何记录是默认的。所以我想要一个解决方案,当其中一个变为 true 时,将其他默认字段设为 false。

字段的 XAML:

<igWPF:Field Name="Default" Width="84">
    <igWPF:Field.Settings>
        <igWPF:FieldSettings CellValuePresenterStyle="{StaticResource ButtonDefault}" />
    </igWPF:Field.Settings>
</igWPF:Field>

CellValuePresenterStyle 的 XAML:

<Style x:Key="ButtonDefault" TargetType="{x:Type igWPF:CellValuePresenter}" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type igWPF:CellValuePresenter}">
                <ToggleButton Content="Default" 
                              HorizontalAlignment="Center" 
                              VerticalAlignment="Center" 
                              FontSize="16" 
                              Width="80" 
                              Height="36"
                              IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

数据来源:

public class LanguageSettingItem : INotifyPropertyChanged
{
    private string name;

    private bool isDefault;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get { return this.name; }
        set { this.name = value; PropChanged("Name"); }
    }
    public bool Default 
    {
        get { return this.isDefault; }
        set { this.isDefault= value; PropChanged("Default"); }
    }

    public void PropChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

【问题讨论】:

    标签: c# wpf infragistics


    【解决方案1】:

    您可以使用相同的事件处理程序选中和取消选中切换按钮事件。 然后只需输入一个 if 条件 - (if toggle_btn.isChecked)'checked code' else 'unchecked code'

    【讨论】:

    • 我试过这个。但不知道为什么它不会触发未选中。
    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多