将 DatePicker 的日期绑定到 ConverterParameter

发布于 2024-11-07 13:35:20 字数 1846 浏览 1 评论 0原文

我正在尝试使用转换器类根据 DatePicker 中的日期来标记 ComboBox 中的名称。

我当前的问题是我不知道如何将 DatePicker 的日期绑定到“ConverterParameter”。有什么建议吗?

(我的代码中可能有更多错误,但我陷入了这一点)

<Page.Resources>  
    <Style TargetType="ComboBoxItem" x:Key="combostyle">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <ControlTemplate.Resources>
                        <src:ColorFromMagazijnierIdConverter x:Key="conv" />
                    </ControlTemplate.Resources>

                    <Grid ToolTip="{Binding Converter={StaticResource conv}, ConverterParameter={ BIND THIS TO THE DATEPICKER DATE }, Mode=OneWay}">
                        <Rectangle x:Name="MarkedItemBackground" IsHitTestVisible="False" Fill="#80FF0000" />
                        <!--...-->
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Converter={StaticResource conv}}"
                                         Value="{x:Null}">
                            <Setter TargetName="MarkedItemBackground" 
                                        Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
<Grid Margin="10,10,10,0" Name="rootGrid">
    <ComboBox Name="collectMagazijnierComboBox"
              DisplayMemberPath="User.Name"
              ItemContainerStyle="{DynamicResource ResourceKey=combostyle}"/>
    <DatePicker Name="collectDatePicker" />
</Grid>

I'm trying to mark Names in a ComboBox based on a Date from a DatePicker using a converter class.

My current problem is I don't know how to bind the date of the DatePicker to the "ConverterParameter". Any suggestions?

(probably more errors in my code but i'm stuck at this point)

<Page.Resources>  
    <Style TargetType="ComboBoxItem" x:Key="combostyle">

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <ControlTemplate.Resources>
                        <src:ColorFromMagazijnierIdConverter x:Key="conv" />
                    </ControlTemplate.Resources>

                    <Grid ToolTip="{Binding Converter={StaticResource conv}, ConverterParameter={ BIND THIS TO THE DATEPICKER DATE }, Mode=OneWay}">
                        <Rectangle x:Name="MarkedItemBackground" IsHitTestVisible="False" Fill="#80FF0000" />
                        <!--...-->
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Converter={StaticResource conv}}"
                                         Value="{x:Null}">
                            <Setter TargetName="MarkedItemBackground" 
                                        Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
<Grid Margin="10,10,10,0" Name="rootGrid">
    <ComboBox Name="collectMagazijnierComboBox"
              DisplayMemberPath="User.Name"
              ItemContainerStyle="{DynamicResource ResourceKey=combostyle}"/>
    <DatePicker Name="collectDatePicker" />
</Grid>

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

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

发布评论

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

评论(1

美胚控场 2024-11-14 13:35:20

ConverterParameter 属性不能是绑定的目标。只有 DependencyObject 的 DependencyProperty 才可以作为绑定的目标。

您需要使用 MultiBinding:

<Grid>
    <Grid.ToolTip>
        <MultiBinding Converter="{StaticResource conv}" Mode="OneWay">
            <Binding /> <!-- this mimics your current binding to the datacontext itself -->
            <Binding ElementName="collectDatePicker" Path="SelectedDate" />
        </MultiBinding>
    </Grid.ToolTip>

    <Rectangle x:Name="MarkedItemBackground" IsHitTestVisible="False" Fill="#80FF0000" />

    <!--...-->

</Grid>

然后需要重写 ColorFromMagazijnierIdConverter 转换器来实现 IMultiValueConverter 接口,您可以在其中访问这两个值。

虽然,我不能 100% 确定您是否可以从样式资源中通过 ElementName 引用collectDatePicker。但你肯定可以玩弄它!

The ConverterParameter property cannot be the target of a binding. Only a DependencyProperty of a DependencyObject can be the target of a binding.

You'll need to use a MultiBinding:

<Grid>
    <Grid.ToolTip>
        <MultiBinding Converter="{StaticResource conv}" Mode="OneWay">
            <Binding /> <!-- this mimics your current binding to the datacontext itself -->
            <Binding ElementName="collectDatePicker" Path="SelectedDate" />
        </MultiBinding>
    </Grid.ToolTip>

    <Rectangle x:Name="MarkedItemBackground" IsHitTestVisible="False" Fill="#80FF0000" />

    <!--...-->

</Grid>

You'll then need to rewrite your ColorFromMagazijnierIdConverter converter to implement the IMultiValueConverter interface instead, in which you can access both values.

Although, I'm not 100% sure whether you can reference the collectDatePicker by ElementName from within the style resource like that. But sure you can play around with it!

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