WPF customcontrol checkbox ischecked 不模板绑定
WPF customcontrol checkbox ischecked doesn't templatebind
我正在制作一个自定义控件,使其像一个过滤器一样,您可以使用复选框启用它,当您这样做时,一些字段会出现供用户填写。
所以我的自定义控件有一个复选框和一个内容控件,我使用内容控件是因为我希望这些字段是我想要的。当我选中该复选框时,我将 contentcontrol 的高度设置为 Auto,当它未被选中时,我将其设置为 0。这很有效。我还需要在我的视图模型中知道该复选框是否被选中。我的问题是,模板绑定似乎不适用于复选框的 IsChecked 属性。例如,我尝试制作一个名为 IsSelected 的依赖项 属性,然后我将复选框的 IsChecked 模板绑定到该 IsSelected 依赖项 属性,但它不起作用。当我选中复选框时,IsSelected 不会更改...
那是我的自定义控件通用 XAML
<Style TargetType="{x:Type local:FilterItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FilterItem}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel>
<CheckBox Name="cbcheck" IsChecked="{TemplateBinding IsSelected}" Content="{TemplateBinding Title}" VerticalContentAlignment="Center"/>
<ContentControl Content="{TemplateBinding Content}" Margin="20 0 5 10">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cbcheck, Path=IsChecked}" Value="False">
<Setter Property="Height" Value="0.0"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=cbcheck, Path=IsChecked}" Value="True">
<Setter Property="Height" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是自定义控件的 .cs,没什么特别的,只有依赖属性
public class FilterItem: Control
{
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(FilterItem), new PropertyMetadata(null));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsSelected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(FilterItem), new PropertyMetadata(false));
public FrameworkElement Content
{
get { return (FrameworkElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(FrameworkElement), typeof(FilterItem), new PropertyMetadata(null));
static FilterItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FilterItem), new FrameworkPropertyMetadata(typeof(FilterItem)));
}
}
我是这样使用它的:
<filtercontrol:FilterItem Title="Diamond Coil" IsSelected="{Binding IsDiamondCoil}">
<filtercontrol:FilterItem.Content>
<StackPanel>
<TextBlock Text="Max Lenght:"/>
<TextBox Text="{Binding MaxCoilOverallLenght}"/>
<TextBlock Text="Min width:"/>
<TextBox/>
<TextBlock Text="Max Width:"/>
<TextBox/>
<TextBlock Text="Max Vertical Section:"/>
<TextBox/>
<TextBlock Text="Max Horizontal Section:"/>
<TextBox/>
</StackPanel>
</filtercontrol:FilterItem.Content>
</filtercontrol:FilterItem>
依赖项的模板绑定属性 我用来设置复选框内容的标题工作得很好。我不明白为什么一个有效而另一个无效。
谢谢。
您可以将 TemplateBinding 替换为 Binding 到 TemplatedParent:
<CheckBox Name="cbcheck" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Content="{TemplateBinding Title}" VerticalContentAlignment="Center"/>
并将您的 DependencyProperty 声明为 TwoWay:
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(FilterItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
我正在制作一个自定义控件,使其像一个过滤器一样,您可以使用复选框启用它,当您这样做时,一些字段会出现供用户填写。 所以我的自定义控件有一个复选框和一个内容控件,我使用内容控件是因为我希望这些字段是我想要的。当我选中该复选框时,我将 contentcontrol 的高度设置为 Auto,当它未被选中时,我将其设置为 0。这很有效。我还需要在我的视图模型中知道该复选框是否被选中。我的问题是,模板绑定似乎不适用于复选框的 IsChecked 属性。例如,我尝试制作一个名为 IsSelected 的依赖项 属性,然后我将复选框的 IsChecked 模板绑定到该 IsSelected 依赖项 属性,但它不起作用。当我选中复选框时,IsSelected 不会更改... 那是我的自定义控件通用 XAML
<Style TargetType="{x:Type local:FilterItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FilterItem}">
<Border Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel>
<CheckBox Name="cbcheck" IsChecked="{TemplateBinding IsSelected}" Content="{TemplateBinding Title}" VerticalContentAlignment="Center"/>
<ContentControl Content="{TemplateBinding Content}" Margin="20 0 5 10">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cbcheck, Path=IsChecked}" Value="False">
<Setter Property="Height" Value="0.0"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=cbcheck, Path=IsChecked}" Value="True">
<Setter Property="Height" Value="Auto"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是自定义控件的 .cs,没什么特别的,只有依赖属性
public class FilterItem: Control
{
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(FilterItem), new PropertyMetadata(null));
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
// Using a DependencyProperty as the backing store for IsSelected. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(FilterItem), new PropertyMetadata(false));
public FrameworkElement Content
{
get { return (FrameworkElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(FrameworkElement), typeof(FilterItem), new PropertyMetadata(null));
static FilterItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FilterItem), new FrameworkPropertyMetadata(typeof(FilterItem)));
}
}
我是这样使用它的:
<filtercontrol:FilterItem Title="Diamond Coil" IsSelected="{Binding IsDiamondCoil}">
<filtercontrol:FilterItem.Content>
<StackPanel>
<TextBlock Text="Max Lenght:"/>
<TextBox Text="{Binding MaxCoilOverallLenght}"/>
<TextBlock Text="Min width:"/>
<TextBox/>
<TextBlock Text="Max Width:"/>
<TextBox/>
<TextBlock Text="Max Vertical Section:"/>
<TextBox/>
<TextBlock Text="Max Horizontal Section:"/>
<TextBox/>
</StackPanel>
</filtercontrol:FilterItem.Content>
</filtercontrol:FilterItem>
依赖项的模板绑定属性 我用来设置复选框内容的标题工作得很好。我不明白为什么一个有效而另一个无效。
谢谢。
您可以将 TemplateBinding 替换为 Binding 到 TemplatedParent:
<CheckBox Name="cbcheck" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Content="{TemplateBinding Title}" VerticalContentAlignment="Center"/>
并将您的 DependencyProperty 声明为 TwoWay:
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), typeof(FilterItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));