在 ItemsControl 内绑定 RadioButton 的 IsChecked 属性

发布于 2024-10-14 06:02:40 字数 826 浏览 1 评论 0原文

我有 ItemsControls 和从 CollectionViewSource 绑定的项目。

    <ItemsControl ItemsSource="{Binding Source={StaticResource VisibleFlagsImageSourcePathView}}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <r:RibbonRadioButton SmallImageSource="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

另一个控制外站:

<TextBox Text="{Binding Path=SelectedCountryCode" />

我想要完成的是每当我更改 TextBox 的值时,我希望相应的 RibbonRadioButton 属性 IsChecked 设置为 true 或 false。

I have ItemsControls with items binded from CollectionViewSource.

    <ItemsControl ItemsSource="{Binding Source={StaticResource VisibleFlagsImageSourcePathView}}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <r:RibbonRadioButton SmallImageSource="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

And another control outsite:

<TextBox Text="{Binding Path=SelectedCountryCode" />

What I am trying to accomplish is whenever I change the value of the TextBox I want the corresponding RibbonRadioButton property IsChecked set to true or false.

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

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

发布评论

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

评论(2

很快妥协 2024-10-21 06:02:40

您需要做的是创建一个具有两个属性的ViewModel

class MyViewModel
{
    // Bind this to TextBox
    public String SelectedCountryCode { get; set; }

    // Bind this to ItemsControl
    public ObservableCollection<Object> VisibleFlagsImageSourcePath { get; set; }
}
// Note I have omitted implementation of `INotifyPropertyChanged`. But, you will need to implement it.

并监视 SelectedCountryCode,只要它发生变化,就更改 VisibleFlagsImageSourcePath 集合中的适当值。

What you need to do is create a ViewModel with two properties.

class MyViewModel
{
    // Bind this to TextBox
    public String SelectedCountryCode { get; set; }

    // Bind this to ItemsControl
    public ObservableCollection<Object> VisibleFlagsImageSourcePath { get; set; }
}
// Note I have omitted implementation of `INotifyPropertyChanged`. But, you will need to implement it.

And monitor the SelectedCountryCode, and whenever it changes, change appropriate value in VisibleFlagsImageSourcePath collection.

寂寞陪衬 2024-10-21 06:02:40

单选按钮代表枚举值。在这种情况下,文本框将代表一个开放值。您似乎想要的是一组开放值以及枚举值的预设选择。最能代表这一点的控件是组合框。

如果您决定继续使用单选按钮/文本框方法,则可以调整人们用于将单选按钮绑定到枚举值的方法,除了使用字符串字段/字符串字段类型转换器而不是枚举字段/枚举字段类型转换器。

有关如何绑定到枚举的信息,请参阅此答案:如何将 RadioButtons 绑定到枚举?

要使其适应字符串,只需创建一个名为 KnownStringToBooleanConverter 的类(请注意,这是与 EnumToBooleanConverter 相同的实现):

public class KnownStringToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

同时创建具有已知字符串的类型(类似于创建枚举的方式):

public static class KnownCountryCodes
{
    // Note: I'm guessing at these codes...
    public const string England = "EN";
    public const string Japan = "JP";
}

然后以类似的方式绑定到此:

<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.England}}" />
<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.Japan}}" />

如果您希望所有控件交叉填充,那么您需要实现 INotifyPropertyChanged 在视图模型中:

public class MyViewModel : INotifyPropertyChanged
{
    // Bind this to TextBox and radio buttons.  Populate the radio buttons manually
    public string SelectedCountryCode
    {
        get
        {
            return selectedCountryCode;
        }
        set
        {
            selectedCountryCode = value;
            RaiseNotifyPropertyChanged("SelectedCountryCode");
        }
    }

    /* Todo: Implement NotifyPropertyChanged and RaiseNotifyPropertyChanged here */

    private string selectedCountryCode;
}

当输入自定义值(不在列表中)时,单选按钮将全部变暗。当您输入列表中的值时,相应的单选按钮将亮起。当您选择正确的单选按钮时,文本框中的值将发生更改。

这种 View/ViewModel 的东西称为 MVVM。
请参阅:http://msdn.microsoft.com/en-us/magazine/ dd419663.aspx

Radio buttons represent enumerated values. A text box in this case would represent an open value. What you seem to want is a set of open values as well as a pre-set selection of enumerated values. The control that best represents this is a combo box.

If you decide to continue with the radio button/text box approach, you can adapt the method people use to bind radio buttons to an enumerated value, except use a string field/string field type converter instead of an enum field/enum field type converter.

See this answer for how to bind to enums: How to bind RadioButtons to an enum?

To adapt this to strings, simply make a class called KnownStringToBooleanConverter (note that this is an identical implementation to EnumToBooleanConverter):

public class KnownStringToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(true) ? parameter : Binding.DoNothing;
    }
}

Also create a type with your known strings (similar to how you would create an enum):

public static class KnownCountryCodes
{
    // Note: I'm guessing at these codes...
    public const string England = "EN";
    public const string Japan = "JP";
}

Then bind to this in a similar way:

<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.England}}" />
<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.Japan}}" />

If you want all your controls to cross-populate, then you'll need to implement INotifyPropertyChanged in your view model:

public class MyViewModel : INotifyPropertyChanged
{
    // Bind this to TextBox and radio buttons.  Populate the radio buttons manually
    public string SelectedCountryCode
    {
        get
        {
            return selectedCountryCode;
        }
        set
        {
            selectedCountryCode = value;
            RaiseNotifyPropertyChanged("SelectedCountryCode");
        }
    }

    /* Todo: Implement NotifyPropertyChanged and RaiseNotifyPropertyChanged here */

    private string selectedCountryCode;
}

When a custom value (that isn't in the list) is entered, the radio buttons will all dim. When you type in a value that is from the list, the corresponding radio button will light up. When you select a correct radio button, the value will be changed in the text box.

This View/ViewModel stuff is called MVVM.
See: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文