将控制值与具有值列表的属性结合。有可能吗?

发布于 2024-12-05 07:11:00 字数 1442 浏览 1 评论 0原文

我有一些窗口,其中有一些控件(时间段选择器):

更新

<ComboBox DisplayMemberPath="{Binding Path=Name}" ItemsSource="{Binding Periods}" Name="timeType" />
<Slider Value="20" Minimum="{Binding SelectedItem.Min, ElementName=timeType}" Maximum="{Binding SelectedItem.Max, ElementName=timeType}" Name="timeSlider" />
<Label Content="{Binding ElementName=timeSlider, Path=Value}" Name="timeValue" />
<Label Content="{Binding ElementName=timeSlider, Path=Minimum}" Name="timeValueMin" />
<Label Content="{Binding ElementName=timeSlider, Path=Maximum}" Name="timeValueMax" />

在窗口类中我做了属性:

    public class TimePeriodType {
        public string Name { set; get; }
        public int Min { set; get; }
        public int Max { set; get; }
    }

    public List<TimePeriodType> Periods = new List<TimePeriodType>() { 
        new TimePeriodType() { Name="Hours", Max=6, Min=1 }, 
        new TimePeriodType(){ Name="Minutes", Max=59, Min=20 }
    };

现在我想做一些事情,当我更改 Groupbox 中的值时更新滑块值。有可能做到吗?

我已经这样做了:

    private void timeType_SelectionChanged( object sender, SelectionChangedEventArgs e ) {
        var period = Periods.Single(p => p.Name == timeType.SelectedValue.ToString());
        timeSlider.Minimum = period.Min;
        timeSlider.Maximum = period.Max;
    }

但对我来说这不是一个很好的解决方案。 也许您知道更简单的方法?

I have windows where I have some controls(time period picker):

UPDATED

<ComboBox DisplayMemberPath="{Binding Path=Name}" ItemsSource="{Binding Periods}" Name="timeType" />
<Slider Value="20" Minimum="{Binding SelectedItem.Min, ElementName=timeType}" Maximum="{Binding SelectedItem.Max, ElementName=timeType}" Name="timeSlider" />
<Label Content="{Binding ElementName=timeSlider, Path=Value}" Name="timeValue" />
<Label Content="{Binding ElementName=timeSlider, Path=Minimum}" Name="timeValueMin" />
<Label Content="{Binding ElementName=timeSlider, Path=Maximum}" Name="timeValueMax" />

In window class I did property:

    public class TimePeriodType {
        public string Name { set; get; }
        public int Min { set; get; }
        public int Max { set; get; }
    }

    public List<TimePeriodType> Periods = new List<TimePeriodType>() { 
        new TimePeriodType() { Name="Hours", Max=6, Min=1 }, 
        new TimePeriodType(){ Name="Minutes", Max=59, Min=20 }
    };

And now I want to do somethink to update Slider values when I change value in Groupbox. Is there any possibility to do it?

Already I do it like this:

    private void timeType_SelectionChanged( object sender, SelectionChangedEventArgs e ) {
        var period = Periods.Single(p => p.Name == timeType.SelectedValue.ToString());
        timeSlider.Minimum = period.Min;
        timeSlider.Maximum = period.Max;
    }

But for me it is not great solution.
Maybe you know simplier way to do it?

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

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

发布评论

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

评论(2

超可爱的懒熊 2024-12-12 07:11:00

我删除了您的设计属性,以使解决方案看起来更清洁。

在您的代码中保留您的timeperiodtype s expect

public List<TimePeriodType> Periods = new List<TimePeriodType>() {
    new TimePeriodType() { Name="Hours",   Max=6,  Min=1  },
    new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
};

。适当设置以允许您绑定到所使用的背景类的成员。

然后,将ListBox(或其他选择器 Control)绑定到该列表:

<ListBox ItemsSource="{Binding Periods}"
         Name="timeType" />

现在,您可以将slider的属性直接绑定到属性selectedItemlistbox(这实际上是timeperiodtype,因为先前的绑定):

<Slider Maximum="{Binding SelectedItem.Max, ElementName=timeType}"
        Minimum="{Binding SelectedItem.Min, ElementName=timeType}"
        Name="timeSlider" />

最后,您可以将标签绑定到分配给slider的值:

<Label Content="{Binding Value, ElementName=timeSlider}"
       Name="timeValue" />
<Label Content="{Binding Minimum, ElementName=timeSlider}"
       Name="timeValueMin" />
<Label Content="{Binding Maximum, ElementName=timeSlider}"
       Name="timeValueMax" />

I removed your design attributes to make the solution look a little cleaner.

Keep your list of TimePeriodTypes exposed in your code:

public List<TimePeriodType> Periods = new List<TimePeriodType>() {
    new TimePeriodType() { Name="Hours",   Max=6,  Min=1  },
    new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
};

Make sure your DataContext is appropriately set to allow you to bind to members of the backing class you're using.

Then, bind the ListBox (or other Selector control) to that list:

<ListBox ItemsSource="{Binding Periods}"
         Name="timeType" />

Now, you can directly bind the properties of the Slider to the properties of the SelectedItem of the ListBox (which is really a TimePeriodType because the of the prior binding):

<Slider Maximum="{Binding SelectedItem.Max, ElementName=timeType}"
        Minimum="{Binding SelectedItem.Min, ElementName=timeType}"
        Name="timeSlider" />

Finally, you can bind the labels to the values assigned to the Slider:

<Label Content="{Binding Value, ElementName=timeSlider}"
       Name="timeValue" />
<Label Content="{Binding Minimum, ElementName=timeSlider}"
       Name="timeValueMin" />
<Label Content="{Binding Maximum, ElementName=timeSlider}"
       Name="timeValueMax" />
对你再特殊 2024-12-12 07:11:00

好吧,我找到了解决方案。
应该有带有 get 方法的属性:

    public class TimePeriodType {
        public string Name { set; get; }
        public int Min { set; get; }
        public int Max { set; get; }
    }

    List<TimePeriodType> _periods = new List<TimePeriodType>() { 
        new TimePeriodType() { Name="Hours", Max=6, Min=1 }, 
        new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
    };

    public List<TimePeriodType> Periods {
        get { return _periods; }
        set { _periods = value; }
    }

另外还应该有以下绑定:

ItemsSource="{Binding Periods, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"

Ok I found solution.
There should be property with get method:

    public class TimePeriodType {
        public string Name { set; get; }
        public int Min { set; get; }
        public int Max { set; get; }
    }

    List<TimePeriodType> _periods = new List<TimePeriodType>() { 
        new TimePeriodType() { Name="Hours", Max=6, Min=1 }, 
        new TimePeriodType() { Name="Minutes", Max=59, Min=20 }
    };

    public List<TimePeriodType> Periods {
        get { return _periods; }
        set { _periods = value; }
    }

And in addition there should be following binding:

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