数据网格中的 4个单选按钮。在Grid中加载时,项目正在加载。但是,如果我更改单选按钮选择,我在datagrid.Itemsource中没有获得价值。所有项目源值都与以前相同。没有发现变化。
<DataGridTemplateColumn Header="Vehicle Type" Width="150" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<RadioButton Grid.Row="0" Grid.Column="0" Template="{StaticResource CustomRadioButtonStyle}" Content="None" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay,
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.None}}"/>
<RadioButton Grid.Row="0" Grid.Column="1" Template="{StaticResource CustomRadioButtonStyle}" Content="Small" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay,
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Small}}"/>
<RadioButton Grid.Row="1" Grid.Column="0" Template="{StaticResource CustomRadioButtonStyle}" Content="Medium" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay,
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Medium}}"/>
<RadioButton Grid.Row="1" Grid.Column="1" Template="{StaticResource CustomRadioButtonStyle}" Content="Heavy" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay,
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Heavy}}"/>
</Grid>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<Window.Resources>
<ObjectDataProvider x:Key="FindCriteria" ObjectType="{x:Type src:enumObjectType}" />
<src:EnumMatchToBooleanConverter x:Key="enumConverter" />
<ControlTemplate TargetType="RadioButton" x:Key="CustomRadioButtonStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<BulletDecorator Background="#00FFFFFF" Height="18">
<BulletDecorator.Bullet >
<mwt:BulletChrome Height="15" Width="15" Background="{TemplateBinding Panel.Background}" BorderBrush="{TemplateBinding Border.BorderBrush}" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderPressed="{TemplateBinding ButtonBase.IsPressed}" IsChecked="{TemplateBinding ToggleButton.IsChecked}" IsRound="True" />
</BulletDecorator.Bullet>
<ContentPresenter Height="15" RecognizesAccessKey="True" Margin="5,0,0,0" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" />
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="ContentControl.HasContent">
<Setter Property="FrameworkElement.FocusVisualStyle">
<Setter.Value>
<Style TargetType="IFrameworkInputElement">
<Style.Resources>
<ResourceDictionary />
</Style.Resources>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" Margin="14,0,0,0" SnapsToDevicePixels="True" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Control.Padding">
<Setter.Value>
<Thickness>4,0,0,0</Thickness>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
public class EnumMatchToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return false;
string checkValue = value.ToString();
string targetValue = parameter.ToString();
return checkValue.Equals(targetValue,
StringComparison.InvariantCultureIgnoreCase);
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value == null || parameter == null)
return null;
bool useValue = (bool)value;
string targetValue = parameter.ToString();
if (useValue)
return Enum.Parse(targetType, targetValue);
return null;
}
}
public class ValidObjectVerificationGrid : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, propertyChangedEventArgs);
}
public int Count { get; set; }
public string ImageURL { get; set; }
private enumObjectType objectType;
public enumObjectType ObjectType
{
get { return objectType; }
set { objectType = value; }
}
public string ObjectDetails { get; set; }
public bool? IsVerified { get; set; }
}
public enum enumObjectType
{
None,
Small,
Medium,
Heavy
}
答案 0 :(得分:1)
我在这台机器上没有Visual Studio,但我注意到ValidObjectVerificationGrid中的setter从不调用你的OnPropertyChanged方法。
试试这个:
public enumObjectType ObjectType
{
get { return objectType; }
set
{
objectType = value;
OnPropertyChanged(new PropertyChangedEventArgs("ObjectType"));
}
}
你应该为每个属性调用OnPropertyChanged,例如ObjectDetails和IsVerified等......
答案 1 :(得分:0)
我试图让你的源代码运行并尝试解决我的问题。您必须在每个单选按钮中使用 RadioButton_Checked 事件。当此事件触发时,捕获所选项,将其转换为datacontext对象,更新所选值,最后重新加载datagrid项目源。而已。我希望它对你有用。
这是一些可能适合您的代码:
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
List<ValidObjectVerificationGrid> fullSource = (List<ValidObjectVerificationGrid>dgValidObjectVerification.ItemsSource;
ValidObjectVerificationGrid x = null;
int index ;
try
{
x = (ValidObjectVerificationGrid)dgValidObjectVerification.SelectedItem;
index = dgValidObjectVerification.SelectedIndex;
}
catch
{
}
if (x!=null)
{
// ... Get RadioButton reference.
var button = sender as RadioButton;
// ... Display button content as title.
this.Title = button.Content.ToString();
if ((Boolean)button.IsChecked)
{
x.ObjectType = (enumObjectType)Enum.Parse(typeof(enumObjectType), button.Content.ToString(), true);
}
foreach (ValidObjectVerificationGrid tmp in fullSource)
{
if (tmp.Count == x.Count)
tmp.ObjectType = x.ObjectType;
}
dgValidObjectVerification.ItemsSource = null;
dgValidObjectVerification.ItemsSource = fullSource;
}
}
这是你的XAML单选按钮部分:
<RadioButton Grid.Row="0" Grid.Column="0" Template="{StaticResource CustomRadioButtonStyle}" Content="None" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay, Checked="RadioButton_Checked"
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.None}}"/>
<RadioButton Grid.Row="0" Grid.Column="1" Template="{StaticResource CustomRadioButtonStyle}" Content="Small" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay, Checked="RadioButton_Checked"
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Small}}"/>
<RadioButton Grid.Row="1" Grid.Column="0" Template="{StaticResource CustomRadioButtonStyle}" Content="Medium" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay, Checked="RadioButton_Checked"
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Medium}}"/>
<RadioButton Grid.Row="1" Grid.Column="1" Template="{StaticResource CustomRadioButtonStyle}" Content="Heavy" FontSize="8" IsChecked="{Binding Path=ObjectType, Mode=TwoWay, Checked="RadioButton_Checked"
Converter={StaticResource enumConverter}, ConverterParameter={x:Static src:enumObjectType.Heavy}}"/>